Multi-Item Rename
Posted: 29 Sep 2008 17:42
XY offers many possibilities when it comes to renaming, but there's one that isn't there and might on occasion be useful to some, the ability to rename a group of items, without any automatic search/replace or anything like that, but specifying each new names individually (Because the current names don't mean nothing, are too different for one pattern to take care of it all, etc)
So this is what this little script will do : select the items, hit Ctrl+Shift+F2 (or whatever KS you assigned to it
) and you'll have the current names listed. Simply edit them as you wish, and press OK to apply. You can also paste in there the names from an external text file, etc
Note that again, a file tmp.ini (in the script's folder) will be used, and removed. So if you already use such a thing and don't want it to be gone, make the necessary adjustments.
Also, you will need to switch recursion checker off or you'll get a lot of warnings...
So this is what this little script will do : select the items, hit Ctrl+Shift+F2 (or whatever KS you assigned to it
Note that again, a file tmp.ini (in the script's folder) will be used, and removed. So if you already use such a thing and don't want it to be gone, make the necessary adjustments.
Also, you will need to switch recursion checker off or you'll get a lot of warnings...
Code: Select all
"Multi-Item Rename... : MultiItemRename"
sub (getinfo("CountSelected") == 0) ? "_mirNoSel" : "_mirStart";
"_mirNoSel"
msg "Nothing selected in the file list.";
"_mirStart"
// del tmp.ini
sub _delTmpIni;
// get current/old names
$old_names = getinfo("SelectedItemsPathNames", "|");
// store them
setkey $old_names, "OldNames", "MultiItemRename", "tmp.ini";
// prepare for editing: remove path
regexreplace $old_names, $old_names, "[^|]+\\([^\\])|", "$1";
// make it multi-line
replace $old_names, $old_names, "|", <crlf>;
// ask user for new names
input $new_names, "Enter the new names for selected items, one name per line", $old_names, m;
// get last 2 bytes
substr $check, $new_names, -2;
// and make sure it ends with a CRLF
$new_names = $new_names . (($check == <crlf>) ? "" : <crlf>);
// switch to one-line
replace $new_names, $new_names, <crlf>, "|";
// and store new names
setkey $new_names, "NewNames", "MultiItemRename", "tmp.ini";
// we only start renaming loop if there is a new name and an old name
$n = ($new_names == "") ? 0 : 1;
$n = ($old_names == "") ? 0 : $n;
sub ($n) ? "_mirLoop" : "_mirDone";
"_mirLoop"
// get list of old names
getkey $old_names, "OldNames", "MultiItemRename", "tmp.ini";
// search for the first item
strpos $old_pos, $old_names, "|";
// and "extract" it
substr $old_name, $old_names, 0, $old_pos;
// get list of new names
getkey $new_names, "NewNames", "MultiItemRename", "tmp.ini";
// search for first item
strpos $new_pos, $new_names, "|";
// and "extract" it
substr $new_name, $new_names, 0, $new_pos;
// remove item from list of old names
substr $old_names, $old_names, $old_pos + 1;
// saves new list
setkey $old_names, "OldNames", "MultiItemRename", "tmp.ini";
// remove item from list of new names
substr $new_names, $new_names, $new_pos + 1;
// saves new list
setkey $new_names, "NewNames", "MultiItemRename", "tmp.ini";
// remove path from old name
regexreplace $check, $old_names, "^.+\\(.+)$", "$1";
// get path of item
regexreplace $old_path, $old_name, "^(.+)\\.+$", "$1";
// locate an item with the new name in the items to be renamed next
strpos $p, "|$check", "|$new_name|";
// "fixing" script: adds "real" rename at the end of the queue: the temp new name (with prefix) as extra old name, with the real new name as new name
self $path, path;
$script_fix = 'getkey $old_names, "OldNames", "MultiItemRename", "'.$path.'\tmp.ini"; getkey $new_names, "NewNames", "MultiItemRename", "'.$path.'\tmp.ini"; setkey $old_names."'.$old_path.'\'.$new_name.'.~mir|", "OldNames", "MultiItemRename", "'.$path.'\tmp.ini"; setkey $new_names."'.$new_name.'|", "NewNames", "MultiItemRename", "'.$path.'\tmp.ini";';
// if found, execute "fixing script"
load ($p > -1) ? $script_fix : 'incr $void;',,s;
// and add prefix for now to avoid collision
$new_name = ($p > -1) ? "$new_name.~mir" : $new_name;
// get old name only (with path)
regexreplace $old_name_only, $old_name, "^.+\\(.+)$", "$1";
// in case no new name was specified, we use the old name
$new_name = ($new_name == "") ? $old_name_only : $new_name;
// rename item
rename b, "$new_name/e",, $old_name;
// get nb of untouched items so far
getkey $nb_untouched, "NbUntouched", "MultiItemRename", "tmp.ini";
// adds 1 if old & new names are the same
$nb_untouched = 0 + (($new_name == $old_name_only) ? $nb_untouched + 1 : $nb_untouched);
// saves nb of untouched items
setkey $nb_untouched, "NbUntouched", "MultiItemRename", "tmp.ini";
// get nb of renamed items
getkey $nb_renamed, "NbRenamed", "MultiItemRename", "tmp.ini";
// adds 1 if old & new names are different AND not a tmp rename (which will be counted later)
$nb_renamed = 0 + (($new_name == $old_name_only) ? $nb_renamed : (($p > -1) ? $nb_renamed : $nb_renamed + 1));
// saves nb of renamed items so far
setkey $nb_renamed, "NbRenamed", "MultiItemRename", "tmp.ini";
// we only continue if there is a new name and an old name
$n = ($new_names == "") ? 0 : 1;
$n = ($old_names == "") ? 0 : $n;
sub ($n) ? "_mirLoop" : "_mirDone";
"_mirDone"
// get nb of untouched and renamed items
getkey $nb_untouched, "NbUntouched", "MultiItemRename", "tmp.ini";
getkey $nb_renamed, "NbRenamed", "MultiItemRename", "tmp.ini";
// del tmp.ini
sub _delTmpIni;
// show usual status message
status "$nb_renamed ".(($nb_renamed == 1) ? "item" : "items")." renamed, $nb_untouched left untouched";
"_delTmpIni"
setkey 1, "FileExists", "Ensure", "tmp.ini";
self $path, path;
delete 0, 0, "$path\tmp.ini";



