GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Swap the names of two files

This rename script (in the form of a toolbar button) will exchange (swap) the names of each pair of selected files.

Because rename scripts only run one file at a time, it does this 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).

See How to add buttons from this forum to your toolbars for information on how to add this button to your toolbars.

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
   <label>Swap Names</label>
   <icon1>#rename</icon1>
   <function type="normal">
      <instruction>Rename PATTERN * TO *</instruction>
      <instruction>@script vbscript</instruction>
      <instruction />
      <instruction>Option Explicit</instruction>
      <instruction />
      <instruction>Dim strPrevName, strTempName</instruction>
      <instruction>Randomize()</instruction>
      <instruction />
      <instruction>Function Rename_GetNewName ( strFileName, strFilePath, _</instruction>
      <instruction>      fIsFolder, strOldName, ByRef strNewName )</instruction>
      <instruction />
      <instruction>   &apos; for the first file of every pair, save the old file and then rename with a random suffix</instruction>
      <instruction>   if Len(strPrevName) = 0 Then</instruction>
      <instruction>      strPrevName = strFileName</instruction>
      <instruction>      strTempName = strNewName &amp; &quot;-temp&quot; &amp; Int(10000 * rnd())</instruction>
      <instruction>   else</instruction>
      <instruction>      Dim oldPath, newPath</instruction>
      <instruction>      Dim fs</instruction>
      <instruction>      Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)</instruction>
      <instruction />
      <instruction>      &apos; rename the first file to a temporary name</instruction>
      <instruction>      oldPath = fs.BuildPath(strFilePath, strPrevName)</instruction>
      <instruction>      newPath = fs.BuildPath(strFilePath, strTempName)</instruction>
      <instruction>      fs.MoveFile oldPath, newPath</instruction>
      <instruction />
      <instruction>      &apos; rename the second file to the first file&apos;s name</instruction>
      <instruction>      oldPath = fs.BuildPath(strFilePath, strFileName)</instruction>
      <instruction>      newPath = fs.BuildPath(strFilePath, strPrevName)</instruction>
      <instruction>      fs.MoveFile oldPath, newPath</instruction>
      <instruction />
      <instruction>      &apos; rename the first file to the second file&apos;s name</instruction>
      <instruction>      oldPath = fs.BuildPath(strFilePath, strTempName)</instruction>
      <instruction>      newPath = fs.BuildPath(strFilePath, strFileName)</instruction>
      <instruction>      fs.MoveFile oldPath, newPath</instruction>
      <instruction>      </instruction>
      <instruction>      Set fs = Nothing</instruction>
      <instruction>      strPrevName = &quot;&quot; &apos; reset for next file</instruction>
      <instruction>   end if</instruction>
      <instruction>   strNewName = strFileName &apos; nop - rename is handled by the FileSystemObject</instruction>
      <instruction>      </instruction>
      <instruction>End Function</instruction>
   </function>
</button>

Thanks, handy enough so i've adopted the button making my original 2-way button a 3-way button. The button works fine for 2 files in the same lister (i.e. both in source lister!) but not across a dual lister. For 1 file selected the button does nothing, and for 3 (or more) files selected the button does some miracle (instead of doing nothing). i do notice a time lag (delay). maybe i can compress the long script code (or the button itself :wink:) with winrar..

Thanks again jon!!