Renaming files in a subdirectory tree

A coworker had an odd little requirement that they asked me if I could help with. She has image files stored in a directory structure, and she wanted to be able to pull the files out and rename them with the parent directories. Confused? I was, too. Essentially they want a file whose path is \A\01\23\45.tif to be renamed to A012345.tif.

Two steps to solving this. First, I needed a script file that would make a copy of a file and rename it by prepending the names of its parent folders to the filename. Second, I needed a script file which would find every file under a given subdirectory and call the first script file for each file found.

The first script file is a Windows Script Host file, written in VBScript. I name this file sdrename.vbs (as in subdirectory rename). The basic flow is:

  1. Retrieve the full, absolute path to the file from command line arguments.
  2. Determine the root folder (the folder that the script file is executed in).
  3. Extract the filename (only) from the full path of the file.
  4. Extract the names of all subfolders between the root folder and the file.
  5. Strip backslashes out of the subfolders extract (step 4).
  6. Determine the new filename (step 4 + step 3).
  7. Copy the file to the root folder.
  8. Rename the copied file to the new filename (step 6).

Option Explicit
'Filenames are required
If WScript.Arguments.Count = 0 Then
  WScript.Echo ">>> Must provide file name!!! <<<"
End If
Dim strFile, strRootFolder, strFilename, strNewFilename, objFSO
WScript.Echo ""
strFile = WScript.Arguments(0)
WScript.Echo "Processing:   " & strFile
strRootFolder = Left( WScript.ScriptFullName, InStr( WScript.ScriptFullName, WScript.ScriptName ) - 1)
WScript.Echo "Root Folder:  " & strRootFolder
strFilename = Mid( strFile, InStrRev( strFile, "\" ) + 1 )
WScript.Echo "Filename:     " & strFilename
strNewFilename = Mid( strFile, Len( strRootFolder ) + 1 )
WScript.Echo "Subfolders:   " & strNewFilename
strNewFilename = Replace( strNewFilename, "\", "" )
WScript.Echo "New Filename: " & strNewFilename
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
objFSO.CopyFile strFile, strFilename
WScript.Echo "File copied successfully."
objFSO.MoveFile strFilename, strNewFilename
WScript.Echo "File renamed successfully!"
WScript.Echo "*** PROCESS COMPLETE ***"

The second script is a simple one-line batch file, which I call renloop.bat (as in rename loop). This calls the shell’s for command to loop through all subdirectories, retrieving the full file name and path for each file that matches the search criteria.

for /r %%i in (%1) do cscript.exe sdrename.vbs "%%i"

To use, copy both script files to the starting directory – the top directory you want to include in the rename. For example, if I wanted to copy and rename all files within c:\mydocs\images\, both files would be copied into this directory. Then, run the following from a command prompt:

renloop *.gif

The result: Every file that matches the search criteria (*.gif) will have the sdrename.vbs script executed against it. Enjoy all your newly created, renamed files!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.