Re: Search and replace "string" in all folder names.
Posted: 22 Apr 2015 11:57
I was wondering if that was the problem. Its rare that commands dont work as the help files say they do.
Forum for XYplorer Users and Developers
https://www.xyplorer.com/xyfc/
Code: Select all
$src = inputfolder(<curpath>, 'Pick root folder');
$patterns = input('Enter the string to remove', "Optionally enter a replacement: string/replace. Suffix \ to replace case-sensitively, eg.: StR/RepLaCe\<crlf 2>Separate additional patterns with a | character", ,'s');
if ($patterns) { //if a pattern was entered then do.
$count = 0;
$Filelst = formatlist(folderreport('files', 'r', $src, 'r', , '|'), 'v', '|'); //Make a string of every file then invert Filelist so deeper folders are processed first
$countAllFiles = gettoken($Filelst, "count", "|"); //Count all Files in the File list
$unrenamedItems = "";
foreach ($itm, $Filelst, '|') { //Do this for each file in the file list and set $itm to next succesive file.
$oldname = gettoken(regexreplace($itm, "(.*)(\.[^.]+$)", "$1"), -1, '\'); //Set $oldname to a single file reading right to left and leave out the file extention part.
$newname = $oldname;
foreach($pattern, $patterns, "|") { //Do this for each pattern in the pattern list and set $pattern to next succesive pattern.
$remstr = gettoken($pattern, 1, '/'); //Set $remstr to part of pattern to be removed.
$rplstr = gettoken($pattern, 2, '/'); //Set $rplstr to part of pattern to be inserted.
$newname = replace($newname, $remstr, $rplstr); //Replace all matching strings to make new name.
}
if ($newname != $oldname) {
$output = renameitem($newname, $itm, 1+4); //Rename file, base only, prompt on name-collision
if !($output) { $unrenamedItems = $unrenamedItems . "$itm<crlf>"; }
else { $count++; }
}
}
$Dirlst = formatlist(folderreport('dirs', 'r', $src, 'r', , '|'), 'v', '|'); //Make a string of every folder then invert Dirlist so deeper folders are processed first
if ($Dirlst) {$Dirlst = $Dirlst.'|'.$src ;} //inclusive of parent folder but not twice for case of no subfolder.
else {$Dirlst = $src ;}
$countAllDirs = gettoken($Dirlst, "count", "|"); //Count all Dirs in the directory list
$unrenamedItems = "";
foreach ($itm, $Dirlst, '|') { //Do this for each path in the directory list and set $itm to next succesive path.
$oldname = gettoken($itm, -1, '\'); //Set $oldname to a single path reading right to left.
$newname = $oldname;
foreach($pattern, $patterns, "|") { //Do this for each pattern in the pattern list and set $pattern to next succesive pattern.
$remstr = gettoken($pattern, 1, '/'); //Set $remstr to part of pattern to be removed.
$rplstr = gettoken($pattern, 2, '/'); //Set $rplstr to part of pattern to be inserted.
$newname = replace($newname, $remstr, $rplstr); //Replace all matching strings to make new name.
}
if ($newname != $oldname) {
$output = renameitem($newname, $itm, 1+2+4); //Rename folder, base and extention, prompt on name-collision
if !($output) { $unrenamedItems = $unrenamedItems . "$itm<crlf>"; }
else { $count++; }
}
}
#1001; //refresh
$message = ($unrenamedItems) ? "<crlf 3>These items could not be renamed:<crlf 2>$unrenamedItems" : "";
text "Items renamed: $count of ($countAllFiles Files + $countAllDirs Folders)$message";
}
Code: Select all
$src = inputfolder("<curpath>", 'Pick root folder');
$type = inputselect("Choose if you want to process files, folders or both...", "Files|Folders|Files & Folders", "|", 4+32+128, , 350, 300);
if ($type == 1) { $type = "files"; }
elseif ($type == 2) { $type = "dirs"; }
elseif ($type == 3) { $type = "items"; }
$lst = formatlist(folderreport($type, 'r', $src, 'r', , '|'), 'v', '|'); // Invert dirlist
if ($type == "dirs" || $type == "items") { $lst = $lst . "|$src"; } // Include root folder
$patterns = input('Enter the string to remove', "Optionally enter a replacement: string/replace. Suffix \ to replace case-sensitively, eg.: StR/RepLaCe\<crlf 2>Separate additional patterns with a | character", ,'s');
if ($patterns) {
$count = 0;
$countAll = gettoken($lst, "count", "|");
$unrenamedItems = "";
foreach ($itm, $lst, '|') {
$lastToken = gettoken($itm, -1, '\');
if (exists($itm) == 2) { $oldname = $lastToken; $ext = ""; }
else { $oldname = regexreplace($lastToken, "(.*?)(\.[^.]+$)", "$1"); $ext = regexreplace($lastToken, "(.*?)(\.[^.]+$)", "$2"); }
$newname = $oldname;
foreach ($pattern, $patterns, "|") {
$remstr = gettoken($pattern, 1, '/');
$rplstr = gettoken($pattern, 2, '/');
$newname = replace($newname, $remstr, $rplstr);
}
if ($newname != $oldname) {
$newname = $newname . $ext;
$output = renameitem($newname, $itm, 1+2+4); //prompt on name-collision
if !($output) { $unrenamedItems = $unrenamedItems . "$itm<crlf>"; }
else { $count++; }
}
}
#1001; //refresh
$message = ($unrenamedItems) ? "<crlf 3>These items could not be renamed:<crlf 2>$unrenamedItems" : "";
text "Items renamed: $count of $countAll$message";
}Code: Select all
$src = inputfolder(<curpath>, 'Pick root folder');
$type = inputselect("Choose if you want to process files, folders or both...", "Files|Folders|Files & Folders", "|", 4+32+128, , 350, 300);
if (($type == 1) or ($type == 2)) {
$ChildDepth = input("Type child folder depth to start renaming inclusive", "Enter value, 0 or enter starts changes at root.", , "s", 0, 250, 150);
}
$RootDepth = gettoken($src, "count", "\");
$countAllFiles = 0;
$countAllDirs = 0;
$patterns = input('Enter the string to remove', "Optionally enter a replacement: string/replace. Suffix \ to replace case-sensitively, eg.: StR/RepLaCe\<crlf 2>Separate additional patterns with a | character", ,'w', ,600);
if ($patterns) { //if a pattern was entered then do.
$count = 0;
$unrenamedItems = "";
if (($type == 0) or ($type == 2)) {
$Filelst = formatlist(folderreport('files', 'r', $src, 'r', , '|'), 'v', '|'); //Make a string of every file then invert Filelist so deeper folders are processed first
$countAllFiles = gettoken($Filelst, "count", "|"); //Count all Files in the File list
foreach ($itm, $Filelst, '|') { //Do this for each file in the file list and set $itm to next succesive file.
$oldname = gettoken(regexreplace($itm, "(.*)(\.[^.]+$)", "$1"), -1, '\'); //Set $oldname to a single file reading right to left.
$newname = $oldname;
foreach($pattern, $patterns, "|") { //Do this for each pattern in the pattern list and set $pattern to next succesive pattern.
$remstr = gettoken($pattern, 1, '/'); //Set $remstr to part of pattern to be removed.
$rplstr = gettoken($pattern, 2, '/'); //Set $rplstr to part of pattern to be inserted.
$newname = replace($newname, $remstr, $rplstr); //Replace all matching strings to make new name.
}
if ($newname != $oldname) {
$output = renameitem($newname, $itm, 1+4); //Rename file, base only, prompt on name-collision
if !($output) { $unrenamedItems = $unrenamedItems . "$itm<crlf>"; }
else { $count++; }
}
}
}
if (($type == 1) or ($type == 2)) {
$Dirlst = formatlist(folderreport('dirs', 'r', $src, 'r', , '|'), 'v', '|'); //Make a string of every folder then invert Dirlist so deeper folders are processed first
if ($Dirlst) {$Dirlst = $Dirlst.'|'.$src ;} //inclusive of parent folder but not twice for case of no subfolder.
else {$Dirlst = $src ;}
$countAllDirs = gettoken($Dirlst, "count", "|"); //Count all Dirs in the directory list
foreach ($itm, $Dirlst, '|') { //Do this for each path in the directory list and set $itm to next succesive path.
$FolderDepth = gettoken($itm, "count", "\");
if ($RootDepth -1 < $FolderDepth - $ChildDepth) { // Incement 0 to stop changes further from the root.
$oldname = gettoken($itm, -1, '\'); //Set $oldname to a single path reading right to left.
$newname = $oldname;
foreach($pattern, $patterns, "|") { //Do this for each pattern in the pattern list and set $pattern to next succesive pattern.
$remstr = gettoken($pattern, 1, '/'); //Set $remstr to part of pattern to be removed.
$rplstr = gettoken($pattern, 2, '/'); //Set $rplstr to part of pattern to be inserted.
$newname = replace($newname, $remstr, $rplstr); //Replace all matching strings to make new name.
}
if ($newname != $oldname) {
$output = renameitem($newname, $itm, 1+2+4); //Rename folder, base and extention, prompt on name-collision
if !($output) { $unrenamedItems = $unrenamedItems . "$itm<crlf>"; }
else { $count++; }
}
}
}
}
#1001; //refresh
$message = ($unrenamedItems) ? "<crlf 3>These items could not be renamed:<crlf 2>$unrenamedItems" : "";
text "Items renamed: $count of ($countAllFiles Files + $countAllDirs Folders)$message";
}