Solved

Remove Special Characters From File Name

Posted on 2013-01-18
8
1,534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 13

Expert Comment

by:Gabriel Clifton
ID: 38794429
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
ID: 38794489
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
ID: 38794618
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:eddiepardon
ID: 38794667
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
 
LVL 13

Assisted Solution

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

Expert Comment

by:Patrick Matthews
ID: 38794728
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
ID: 38794767
Right, I missed that. Not paying attention i guess.
0
 

Expert Comment

by:joe Bloger
ID: 41839863
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Configuring Remote Assistance for use with SCCM
A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

695 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