Solved

Remove Special Characters From File Name

Posted on 2013-01-18
8
1,459 Views
Last Modified: 2016-10-12
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!
0
Comment
Question by:eddiepardon
8 Comments
 
LVL 13

Expert Comment

by:Gabriel Clifton
Comment Utility
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
0
 

Author Comment

by:eddiepardon
Comment Utility
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
0
 
LVL 13

Accepted Solution

by:
Gabriel Clifton earned 500 total points
Comment Utility
try this batch script

@echo off

setlocal enabledelayedexpansion
for /f "usebackq delims=" %%N in (`dir /s /b`) do (
  set var=%%~nN
  set var=!var:^&= !
  set var=!var:%%= !

  if not "!var!"=="%%~nN" (
    if not exist "%%~dpN!var!%%~xN" (
      echo "%%N" --^> "!var!%%~xN"
      ren "%%N" "!var!%%~xN"
    ) else (
      echo File "!var!%%~xN" ^(from %%N^) already exists.
    )
  )
)
0
 

Author Comment

by:eddiepardon
Comment Utility
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?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 13

Assisted Solution

by:Gabriel Clifton
Gabriel Clifton earned 500 total points
Comment Utility
set var=!var:^&= !
 set var=!var:^(= !
 set var=!var:^?= !
and so on
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
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

0
 
LVL 13

Expert Comment

by:Gabriel Clifton
Comment Utility
Right, I missed that. Not paying attention i guess.
0
 

Expert Comment

by:joe Bloger
Comment Utility
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'
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now