?
Solved

Remove Special Characters From File Name

Posted on 2013-01-18
8
Medium Priority
?
1,563 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 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Introduction to Processes
Suggested Courses

801 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