Riain McAtamney
Riain McAtamney

Reputation: 6422

Script to remove special characters from filenames

I have a folder containing a large number of files. A lot of the filenames have '%' and/or '&' characters in them.

e.g. Test&doc.pdf
e.g Test%doc.doc

Is there a quick way I could remove the '%' and '&' characters using a windows batch file, vbscript or something similar?

All help will be greatly appreciated.

Thanks.

Upvotes: 4

Views: 13315

Answers (3)

Jules
Jules

Reputation: 1371

I've quickly thrown that together and didn't test it, but this VBScript should do the trick. Tell me if you need fancy stuff like folder recursive replacing etc.

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

Upvotes: 2

jeb
jeb

Reputation: 82202

@indiv

If someone can produce a batch solution without character limitations, I'll be massively impressed.

Ok, I'll try.

@echo off

setlocal DisableDelayedExpansion
for /f "usebackq delims=" %%N in (`dir /s /b`) do (
  set var=%%~nxN
  setlocal EnableDelayedExpansion
  set "org=!var!"
  set "var=!var:&= !"
  set "var=!var:%%= !"

  if not "!var!"=="!org!" (
    if not exist "%%~dpN!var!" (
      echo "!org!" --^> "!var!"
      ren "!org!" "!var!"
    ) else (
      echo File "!var!" ^(from !org!^) already exists.
    )
  )
  endlocal
)

The trick is, to toggle the delayed expansion, because expanding of for-loop-vars (%%N) should be done without delayed expansion, else you lose the exclamation marks, and got problems with carets. But to handle and modify the strings you should use delayed expansion.

But why? The answer is to understand the phases of the batch parser.

I tried to explain it here. how-does-the-windows-command-interpreter-cmd-exe-parse-scripts

Upvotes: 3

indiv
indiv

Reputation: 17846

Here's how you can do it in batch (in case you're curious). The big limitation is that if you have filenames with more than one percent sign, it won't work because the shell expands it to a variable. I don't know immediately how to fix that.

It starts from whatever directory the script is in, and works recursively over all subdirectories.

@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.
    )
  )
)

E.g., prints output like this:

C:\batch\schar>schar
"C:\batch\schar\Test%doc.doc" --> "Test doc.doc"
"C:\batch\schar\Test%doc.pdf" --> "Test doc.pdf"
File "Test doc.pdf" (from C:\batch\schar\Test&doc.pdf) already exists.
"C:\batch\schar\subdir\FILE%here" --> "FILE here"

Upvotes: 3

Related Questions