Link to home
Start Free TrialLog in
Avatar of details212
details212

asked on

Batch file to remove character in file name

need a batch file to loop through a bunch or jpg in a folder and remove ! and # and " " ,<-- that's a space in the file names

Many thanks
Avatar of Bill Prew
Bill Prew

The exclamation mark will make this fairly difficult if not impossible in DOS BAT.  I'd suggest just using a GUI utility like one of these.

http://www.den4b.com/?x=products&product=renamer

http://www.bulkrenameutility.co.uk/Main_Intro.php

~bp
This should do the trick:

for %a in (*) do @for /F "tokens=1,2,3,4,5,6,7,8,9* delims=#! " %i in ("%a") do ren "%a" "%i%j%k%l%m%n%o%p%q%r"

Open in new window


Add another "echo" to see what it will do without actually executing the renames:

c:\temp\foo>for %a in (*) do @for /F "tokens=1,2,3,4,5,6,7,8,9* delims=#! " %i in ("%a") do @echo ren "%a" "%i%j%k%l%m%n%o%p%q%r"
ren "a!b" "ab"
ren "a!b .txt" "ab.txt"
ren "e!f#g h.txt" "efgh.txt"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sjklein42
sjklein42
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of details212

ASKER

Wow, awsome
I can't see this working in DOS nor CMD... Unless of course, powershell does what DOS does not...

As billprew says, the exclamation mark is a problem...

Here's a quick test:

   COPY NUL "file!! ##.jpg"

Sure, you can see the file with a DIR command... You can also rename the file ie,

   REN "file!! ##.jpg" file.jpg

What you can't do though is this:

   FOR %%a IN (*.jpg) DO ECHO %%a

It will show %%a as:

   file ##.jpg

instead of:

   file!! ##.jpg

Please confirm if this is not the case.
Works for me.  Did you even try it?  What makes you think the exclamation point is special?

c:\temp\foo>echo foo >a!b.jpg

c:\temp\foo>for %a in (*.jpg) do echo %a

c:\temp\foo>echo a!b.jpg
a!b.jpg

c:\temp\foo>

Open in new window

worked fine for me in win7 cmd