Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to remove special characters from filenames

Tags:

batch-file

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.

like image 418
Riain McAtamney Avatar asked Feb 26 '23 14:02

Riain McAtamney


2 Answers

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"
like image 117
indiv Avatar answered Feb 28 '23 05:02

indiv


@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

like image 39
jeb Avatar answered Feb 28 '23 03:02

jeb