«

»

Mar 09 2009

Renaming multiple folders

I might go a few weeks or months before I download photos from our various digital cameras. I sort all my images into daily folders but the organization/connection software I use doesn’t allow me to create folder names with the structure I prefer (I hate spaces in file/folder names). I might end up with 50+ total folders (or more) that I have to rename manually – needless to say, it’s a time consuming hassle.


I just needed a small script to run a character string search on each sub folder (recursive) and replace one string with another. Not complicated at all but definately not worth $20-40 for a commercial application that wraps vbscript/batch processing in a neat UI shell. A little help from Google and some quick research on folder/file objects within vbscript and I came up with a solution that works perfectly:

Dim Folder_input, Find_Str, Replace_Str

Folder_input = inputbox(“Enter the root folder path:” & vbcrlf & “e.g. C:\wutemp”)
If Folder_input = “” then App_Quit

Find_Str = inputbox(“Search for:”)
If Find_Str = “” then App_Quit

Replace_Str = inputbox(“Replace with:”)
If Replace_Str = “” then App_Quit

Set objFSO= createobject(“Scripting.FileSystemObject”)

Process_Folders Folder_input

Sub Process_Folders(byval Folder)
Dim objFolder, colSubfolders, New_Folder
Set objFolder = objFSO.GetFolder(Folder)
Set colSubfolders = objFolder.Subfolders

For each objSubfolder in colSubfolders
New_Folder = (Replace(objSubfolder.name, find_str, replace_str))
If New_Folder <> objSubFolder.Name Then
objSubFolder.Name = New_Folder
End If
Process_Folders objSubfolder.path
Next

End Sub

Sub App_Quit
wscript.quit
End Sub

To make it even easier to run from my desktop, I created a simple batch file that fires off the script:

d:
cd\wutemp
cscript folder_rename.vbs
exit

The replace method is case sensitive but otherwise it will take any combination of letters/symbols and replace them with whatever you specific for each folder name within the root folder you declare at the beginning of the script.

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>