I inherited a folder with a bunch of files in it. Almost every file had spaces in the name. I abhor spaces in file names. Also, I need to put these files in Subversion, so the names would have all be encoded so they’d work as URLs. That is, all of the spaces would become %20 or some such thing. After manually fixing the first one, I opted for a script.
First, I set a reference to “Microsoft Scripting Runtime” (VBE – Tools – References) so I could access the FileSystemObject.
Dim fso As FileSystemObject
Dim fsoFile As File
Set fso = New FileSystemObject
For Each fsoFile In fso.GetFolder(“C:Tester”).Files
If InStr(1, fsoFile.Name, ” “) > 0 Then
fsoFile.Name = Replace(fsoFile.Name, ” “, “_”)
I had to test to make sure there was a space in the file name. If there wasn’t, it complained that the file already exists. So it must copy, rename, then delete.