Search and replace "string" in all folder names.
-
stts
- Posts: 18
- Joined: 13 Apr 2006 06:33
Re: Search and replace "string" in all folder names.
I was wondering if that was the problem. Its rare that commands dont work as the help files say they do.
-
stts
- Posts: 18
- Joined: 13 Apr 2006 06:33
Re: Search and replace "string" in all folder names.
I had problems with the script with the file extensions. I usually rename all the periods in file names to spaces but not at the extension. The script was taking them all out. So the extension had no periods and the extension name was being added to the base name resulting in 2 extensions. You guys have written enough of the code to get me over the hump so I can do some coding on my own. And so I worked on the file extension problem myself till I got it to work. That way I wont pestor you for every little thing. I dont have the menu part in it since my version of XYplorer cant run it. But its easy to add since I seperated the files renaming from the directory renaming. 
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";
}
-
highend
- Posts: 14950
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Search and replace "string" in all folder names.
This fixes the problems when replacing dots with spaces for file extensions (and I fixed another small error, too):
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";
}One of my scripts helped you out? Please donate via Paypal
-
stts
- Posts: 18
- Joined: 13 Apr 2006 06:33
Re: Search and replace "string" in all folder names.
Well its clear I took the scenic route, but I did learn a thing or 2 or 3 on this task of cleaning up all my file names. And using this little script with a text file of pre layed out rename tasks, I can rename all future files as I rename these without giving it a second thought. I very much appreciate your help. 
-
stts
- Posts: 18
- Joined: 13 Apr 2006 06:33
Re: Search and replace "string" in all folder names.
I made a new feature. I now can pick the child folder depth where directory changes will start. This way all directories to clean up can be put under one root. The root dont need changing because its just the convenience root, and I dont want the directory info to be changed. Just the childs of all those directories is what I want to clean up. They have repeated info thats already part of the parent name. Same with the next child. So if the root everthing is under is 0, then I want my changes to start in level 2. Directory authors seem to want to put their name in every subdirectory. Making for huge huge paths. Now in one fell swoop, I can weed out various pieces of info from various levels in the directory structure. The parent has all the info, and all the repeat crap is weeded out of all the childs. I have now cleaned up about 10 Terrabytes of crazy long directory names. Works good. 
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";
}
XYplorer Beta Club