eddiepardon
asked on
Remove Special Characters From File Name
Hello Experts!
I need a script, either batch or vbscript, that will loop through all files and folders/sub Folders in C:\Upload and remove all of the characters listed below from their filenames.
? " # % & * : < > \ { | } ~
Thanks In Advance!
I need a script, either batch or vbscript, that will loop through all files and folders/sub Folders in C:\Upload and remove all of the characters listed below from their filenames.
? " # % & * : < > \ { | } ~
Thanks In Advance!
ASKER
Panther,
Thanks for your response.
Based on what you provided I am getting an error "Object doesn't support this property or method:" on Line 14 Char 5...
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
'Change Folder as Needed to point to the correct path
objStartFolder = "C:\Users\pardone\Desktop\ SR's\SR 1171 CLM Servers Project"
Set objFolder = objFSO.GetFolder(objStartF older)
Set regEx = New RegExp
'Your pattern here
regEx.Pattern = "[?""#%&*:<>\{}|~]"
Set colFiles = objFolder.Files
For Each objFile in colFiles
objFile.Rename(regEx.Repla ce(objFile .Name, ""))
Next
Thanks for your response.
Based on what you provided I am getting an error "Object doesn't support this property or method:" on Line 14 Char 5...
Set objFSO = CreateObject("Scripting.Fi
'Change Folder as Needed to point to the correct path
objStartFolder = "C:\Users\pardone\Desktop\
Set objFolder = objFSO.GetFolder(objStartF
Set regEx = New RegExp
'Your pattern here
regEx.Pattern = "[?""#%&*:<>\{}|~]"
Set colFiles = objFolder.Files
For Each objFile in colFiles
objFile.Rename(regEx.Repla
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It looks like this will only resolve for the 2 characters (&%)
How do I handle the others ( ? " # * : < > \ { | } ~)?
Also I do not understand how the path is defined... does the batch file have to reside in the folder that you want to search?
How do I handle the others ( ? " # * : < > \ { | } ~)?
Also I do not understand how the path is defined... does the batch file have to reside in the folder that you want to search?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As to the VBScript error, the File object in the Scripting Runtime library does not have a Rename method. You would have to use the Move method.
Dim objFSO, objStartFolder, objFile, regEx
Const StartPath = "C:\Folder\Subfolder"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(StartPath)
Set regEx = New RegExp
'Your pattern here
regEx.Pattern = "[?""#%&*:<>\{}|~]"
Set colFiles = objFolder.Files
For Each objFile in colFiles
objFile.Move regEx.Replace(objFile.Name, "")
Next
Set regEx =Nothing
Set objFile = Nothing
Set objStartFolder = Nothing
Set objFSO = Nothing
Right, I missed that. Not paying attention i guess.
This doesn't work if there is more than one special character in the filename? also what would you have to do to replace the special character with normal chars. e.g. 'file&Folder.txt' to 'file and folder.txt'
Set objFSO = CreateObject("Scripting.Fi
'Your folder here
objStartFolder = "X:\MYFOLDER"
Set objFolder = objFSO.GetFolder(objStartF
Set regEx = New RegExp
'Your pattern here
regEx.Pattern = "[&%]"
Set colFiles = objFolder.Files
For Each objFile in colFiles
objFile.Rename(regEx.Repla
Next