Link to home
Start Free TrialLog in
Avatar of eddiepardon
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!
Avatar of Gabriel Clifton
Gabriel Clifton
Flag of United States of America image

VBSCRIPT

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Your folder here
objStartFolder = "X:\MYFOLDER"

Set objFolder = objFSO.GetFolder(objStartFolder)
Set regEx = New RegExp

'Your pattern here
regEx.Pattern = "[&%]"

Set colFiles = objFolder.Files
For Each objFile in colFiles
    objFile.Rename(regEx.Replace(objFile.Name, "")
Next
Avatar of eddiepardon
eddiepardon

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.FileSystemObject")

'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(objStartFolder)
Set regEx = New RegExp

'Your pattern here
regEx.Pattern = "[?""#%&*:<>\{}|~]"

Set colFiles = objFolder.Files
For Each objFile in colFiles
    objFile.Rename(regEx.Replace(objFile.Name, ""))
Next
ASKER CERTIFIED SOLUTION
Avatar of Gabriel Clifton
Gabriel Clifton
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Patrick Matthews
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

Open in new window

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'