Page 1 of 1
Swap file names -- don't swap extensions?
Posted: 05 Aug 2011 18:49
by myrnalarson
With version 9.90.1000 when swapping the names of 2 files, you are allowed to select files with different extensions. The swap includes the extension. Can this be changed to leave the extension alone, as most of the options on the Rename Special menu do?
I've gottem myself into much trouble by swapping the names of, say a JPG file and a PSD file. When XYPlorer was in thumbnail view, and I was looking at a folder with almost 300 pictures (JPG and PSD, a few TIF's) and it hit a misnamed file -- a JPG file that had a PSD extension. This hung XYPlorer at the point of generating its thumbnail, and I had to shut the program down from Task Manager. I'd be embarassed to tell you how long it took me to figure out the problem and fix it.
Re: Swap file names -- don't swap extensions?
Posted: 05 Aug 2011 19:43
by highend
Don't know if Don will change that.
I just wrote a quick and dirty script that you can use for a toolbar button or anything else that can invoke a script:
It checks if you really! selected a pair of folders or files (not a mix of them ofc) and will retain the original extension for files. The first selected item gets a temporary name to avoid name conflicts (if files have the same extension or for folders in general).
Code: Select all
/* 06.08.2011, swap the names of two files. Leave the extension alone
::load "<xyscripts>\SwapFileNames.xys";
Remarks: Two files or two folders on one pane must be selected
*/
setting('BackgroundFileOps', 0);
// Get current path
$CurrentPath = "<curpath>";
// Get selected items
$SelectedFilesOrFolders = get("SelectedItemsPathNames", "|");
// If nothing || one file / folder || more than two files / folders are selected, abort script
end (getinfo("CountSelected") <= 1 || getinfo("CountSelected") >= 3), "You haven't selected a pair of files or folders, aborted!";
// Check if a single | multiple files, a folder or a drive is selected
// False = folder | True = file | Drive = drive
$CheckFileOrFolder = report("{Dir False|True|Drive}|", 1);
$CheckItem1 = gettoken("$CheckFileOrFolder", 1, "|");
$CheckItem2 = gettoken("$CheckFileOrFolder", 2, "|");
end($CheckItem1 != $CheckItem2), "Your selected items are not a file or folder pair!, aborted";
if("$CheckItem1" == "True" && "$CheckItem2" == "True") {
// A pair of files is selected
// Get the base name of the two files
$FirstSelectedItem = gettoken("$SelectedFilesOrFolders", 1, "|");
$FileBaseName1 = regexreplace($FirstSelectedItem, "(.*\\)(.*(?=\.))(.*)", "$2");
$Extension1 = regexreplace($FirstSelectedItem, "(.*\.)(.*)", "$2");
$SecondSelectedItem = gettoken("$SelectedFilesOrFolders", 2, "|");
$FileBaseName2 = regexreplace($SecondSelectedItem, "(.*\\)(.*(?=\.))(.*)", "$2");
// First file has to get a temp name before the real rename occurs if the file extension is the same!
$TempFileBaseName1 = ~TempFileName1;
$NewFirstSelectedItem = $CurrentPath . "\" . $TempFileBaseName1 . "." . $Extension1;
// Rename them
rename s, "$FileBaseName1/$TempFileBaseName1", , "$FirstSelectedItem";
rename s, "$FileBaseName2/$FileBaseName1", , "$SecondSelectedItem";
rename s, "$TempFileBaseName1/$FileBaseName2", , "$NewFirstSelectedItem";
} else {
// A pair of folders is selected
// Get the base name of the two folders
$FirstSelectedItem = gettoken("$SelectedFilesOrFolders", 1, "|");
$FolderBaseName1 = regexreplace($FirstSelectedItem, "(.*\\)(.*)", "$2");
$SecondSelectedItem = gettoken("$SelectedFilesOrFolders", 2, "|");
$FolderBaseName2 = regexreplace($SecondSelectedItem, "(.*\\)(.*)", "$2");
// First folder has to get a temp name before the real rename occurs
$TempFolderBaseName1 = ~TempFolderName1;
$NewFirstSelectedItem = $CurrentPath . "\" . $TempFolderBaseName1;
// Rename them
rename s, "$FolderBaseName1/$TempFolderBaseName1", , "$FirstSelectedItem";
rename s, "$FolderBaseName2/$FolderBaseName1", , "$SecondSelectedItem";
rename s, "$TempFolderBaseName1/$FolderBaseName2", , "$NewFirstSelectedItem";
}
Re: Swap file names -- don't swap extensions?
Posted: 24 Aug 2011 13:59
by admin
myrnalarson wrote:With version 9.90.1000 when swapping the names of 2 files, you are allowed to select files with different extensions. The swap includes the extension. Can this be changed to leave the extension alone, as most of the options on the Rename Special menu do?
This makes some sense. Maybe I do something later. Actually I never got much feedback on the swap function (although myself I use it quite often -- always on files with same extension I think).
Re: Swap file names -- don't swap extensions?
Posted: 24 Aug 2011 14:26
by TheQwerty
My biggest use of swap file names is actually more about swapping extensions than anything else:
file.ext <> file.ext.bak
Re: Swap file names -- don't swap extensions?
Posted: 24 Aug 2011 14:30
by admin
TheQwerty wrote:My biggest use of swap file names is actually more about swapping extensions than anything else:
file.ext <> file.ext.bak
Using bak as extension is crime in itself of course.

Re: Swap file names -- don't swap extensions?
Posted: 24 Aug 2011 15:18
by j_c_hallgren
admin wrote: Actually I never got much feedback on the swap function (although myself I use it quite often -- always on files with same extension I think).
It's one of the function that I LOVE in XY and use it fairly regularly, as I run a couple of log files thru a VBS creating two files from each (normal data and error data) and then need to swap the "normal" file and original input names so I can upload the trimmed log data to website.
Re: Swap file names -- don't swap extensions?
Posted: 24 Aug 2011 17:50
by myrnalarson
highend wrote:
....I just wrote a quick and dirty script that you can use for a toolbar button or anything else that can invoke a script....
Thanks
VERY MUCH for the script. I've been programming for more than 50 years, first in assembler, then Apple Basic, then DOS Basic, then Visual Basic and VBA, plus a few java script things for my sound editor, but I haven't found enough XYPlorer examples to get a handle on what Microsoft calls the "object model".
I see that from studying your script I'll be able to get started. Thanks again!
Re: Swap file names -- don't swap extensions?
Posted: 24 Aug 2011 17:51
by highend
Thanks VERY MUCH for the script
You're welcome.