?
Solved

Remove Special Characters From File Name

Posted on 2013-01-18
8
Medium Priority
?
1,666 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
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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 total points
ID: 38794720
set var=!var:^&= !
 set var=!var:^(= !
 set var=!var:^?= !
and so on
0
 
LVL 93

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
Unable to change the program that handles the scan event from a network attached Canon/Brother printer/scanner. This means you'll always have to choose which program handles this action, e.g. ControlCenter4 (in the case of a Brother).
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

839 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