This rename script (in the form of a toolbar button) will exchange (swap) the names of each pair of selected files or folders.
See the Button .dcf Files under How to use buttons and scripts from this forum for how to add .dcf files to your toolbars.
New version:
This is an updated version for Opus 12 which does things in a cleaner way, and with support for logging what was done.
Undo doesn't currently work (also an issue with the old version). For now, the best way to undo a swap is to simply run it again and swap things back.
Two versions are provided:
-
One which preserves the extension of each file.
Swap File Names (preserve extensions).dcf (2.9 KB)
For example, if you want
A.jpg
andB.txt
to become
B.jpg
andA.txt
, respectively. -
Another which swaps the whole names, including extensions.
Swap File Names (including extensions).dcf (2.9 KB)
For example, if you want
A.jpg
andA.jpg.bak
to become
A.jpg.bak
andA.jpg
, respectively.
Script code for reference:
This code is contained in the .dcf files above. It's only reproduced here so you can see how it works without downloading anything.
Click either section to expand it.
Preserve extensions
function OnClick(clickData)
{
var sourceTab = clickData.func.sourcetab;
// Go through the selected items in pairs.
var eSel = new Enumerator(clickData.func.sourcetab.selected);
while (!eSel.atEnd())
{
var fileA = eSel.item();
eSel.moveNext();
if (eSel.atEnd())
break; // Odd number of files selected.
var fileB = eSel.item();
eSel.moveNext();
SwapFiles(sourceTab, fileA, fileB);
}
}
function SwapFiles(sourceTab, fileA, fileB)
{
// Swap two files' names, preserving each file's extension.
var cmd = DOpus.Create.Command();
cmd.SetSourceTab(sourceTab);
cmd.deselect = false;
var nameA = fileA.name_stem_m;
var nameB = fileB.name_stem_m;
cmd.AddLine("Rename PATTERN * TO *");
cmd.AddLine("@script jscript");
// Heard you liked scripts, so we put a script inside your script.
cmd.AddLine('function OnGetNewName(data) {');
cmd.AddLine(' if (data.newname_stem_m == "' + nameA + '")');
cmd.AddLine(' return "' + nameB + '" + data.newname_ext_m;');
cmd.AddLine(' if (data.newname_stem_m == "' + nameB + '")');
cmd.AddLine(' return "' + nameA + '" + data.newname_ext_m;');
cmd.AddLine('}');
cmd.AddFile(fileA);
cmd.AddFile(fileB);
cmd.Run();
}
Swap whole names
function OnClick(clickData)
{
var sourceTab = clickData.func.sourcetab;
// Go through the selected items in pairs.
var eSel = new Enumerator(clickData.func.sourcetab.selected);
while (!eSel.atEnd())
{
var fileA = eSel.item();
eSel.moveNext();
if (eSel.atEnd())
break; // Odd number of files selected.
var fileB = eSel.item();
eSel.moveNext();
SwapFiles(sourceTab, fileA, fileB);
}
}
function SwapFiles(sourceTab, fileA, fileB)
{
// Swap two files' names, including each file's extension.
var cmd = DOpus.Create.Command();
cmd.SetSourceTab(sourceTab);
cmd.deselect = false;
var nameA = fileA.name;
var nameB = fileB.name;
cmd.AddLine("Rename PATTERN * TO *");
cmd.AddLine("@script jscript");
// Heard you liked scripts, so we put a script inside your script.
cmd.AddLine('function OnGetNewName(data) {');
cmd.AddLine(' if (data.newname == "' + nameA + '")');
cmd.AddLine(' return "' + nameB + '";');
cmd.AddLine(' if (data.newname == "' + nameB + '")');
cmd.AddLine(' return "' + nameA + '";');
cmd.AddLine('}');
cmd.AddFile(fileA);
cmd.AddFile(fileB);
cmd.Run();
}
Old version:
This was about the only way to do the same thing before Opus 12. The newer methods above are better unless you're on an old version.
This version will swap the whole names, including extensions.
Because rename scripts only run one file at a time, it works by remembering the name of the first file of every pair, and then doing the actual rename when processing the second file of every pair. The rename itself is implemented as a three-step process: 1) first file to temporary name, 2) second file to first file name, 3) temporary name to second file name.
There's no error checking (although it could be added) and because the script uses the FileSystemObject to do the actual rename, rather than Opus itself, there's no way to undo the rename (other than running it again to swap the names back again).
Swap Names (OLD VERSION).dcf (2.7 KB)
Code for old version
Rename PATTERN * TO *
@script vbscript
Option Explicit
Dim strPrevName, strTempName
Randomize()
Function Rename_GetNewName ( strFileName, strFilePath, _
fIsFolder, strOldName, ByRef strNewName )
' for the first file of every pair, save the old file and then rename with a random suffix
if Len(strPrevName) = 0 Then
strPrevName = strFileName
strTempName = strNewName & "-temp" & Int(10000 * rnd())
else
Dim oldPath, newPath
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
' rename the first file to a temporary name
oldPath = fs.BuildPath(strFilePath, strPrevName)
newPath = fs.BuildPath(strFilePath, strTempName)
fs.MoveFile oldPath, newPath
' rename the second file to the first file's name
oldPath = fs.BuildPath(strFilePath, strFileName)
newPath = fs.BuildPath(strFilePath, strPrevName)
fs.MoveFile oldPath, newPath
' rename the first file to the second file's name
oldPath = fs.BuildPath(strFilePath, strTempName)
newPath = fs.BuildPath(strFilePath, strFileName)
fs.MoveFile oldPath, newPath
Set fs = Nothing
strPrevName = "" ' reset for next file
end if
strNewName = strFileName ' nop - rename is handled by the FileSystemObject
End Function