Skip to content

Instantly share code, notes, and snippets.

@JoshMock
Created July 14, 2010 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshMock/475676 to your computer and use it in GitHub Desktop.
Save JoshMock/475676 to your computer and use it in GitHub Desktop.
Given a folder full of files, replaces one string for another in each file name.
' Results: Looks at the names of all files in FOLDER_TO_SCAN, replacing any instance of STRING_TO_REPLACE with REPLACE_WITH in each filename.
Dim FOLDER_TO_SCAN, STRING_TO_REPLACE, REPLACE_WITH
FOLDER_TO_SCAN = "C:\Folder\to\scan\"
STRING_TO_REPLACE = "replace this"
REPLACE_WITH = "with this"
Dim fso, folder
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(FOLDER_TO_SCAN)
For Each subfolder In folder.SubFolders
If Instr(subfolder.Name, STRING_TO_REPLACE) > 0 Then
subfolder.name = Replace(subfolder.Name, STRING_TO_REPLACE , REPLACE_WITH)
End If
Next
For Each file In folder.Files
If Instr(file.Name, "#") > 0 Then
file.name = Replace(file.Name, "#", "")
End If
Next
Set fso = Nothing
Set folder= Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment