Commas crash batch renaming?

December 2, 2009 at 04:49:19
Specs: Windows XP
Hi all,

I was batch-renaming my files to replace all instances of "%20" with " ".

This makes files like this:

TR%2010%20Construction%20Report.pdf

look like this

TR 10 Construction Report.pdf


I found-and-edited a suggestion previously posted on this site. The batch file code is below:

**** CODE START ****

@echo off
setlocal EnableDelayedExpansion

rem Change path as required /s All files
dir /b /s | find "%%20" > Files.txt

for /f %%a in (Files.txt) do (
set FName=%%a
set FName=!FName:%%20=" "!

copy %%a !FName! > nul
del %%a > nul
)
echo Done!
setlocal

**** CODE END ****

My problem is that if this comes across a file with a comma in the filename, it dies.

So anything like this is a no-no:

TR%2010%20Construction,%20Report.pdf

My batch-file programming experience doesn't include much experience with these fancy variable manipulations - so can anyone suggest a change to avoid this comma-causing-coma?

Any thoughts or suggestions are appreciated.

- Steve.


See More: Commas crash batch renaming?

Report •


#1
December 2, 2009 at 06:01:55
To activate, edit the ECHO out of the last two lines.

========================
@echo off & setLocal enableDELAYedexpansion

set P=%%%%20

for /f "tokens=*" %%a in ('dir/b ^| find "%%20"') do (
set F=%%a
set F=!F:%P%= !
echo copy %%a !F!
echo del %%a
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
December 2, 2009 at 10:34:53
oops... did that before coffee
============================
@echo off & setLocal enableDELAYedexpansion

set P=%%%%20

for /f "tokens=*" %%a in ('dir/b ^| find "%%20"') do (
set F=%%a
set F=!F:%P%= !
echo copy "%%a" "!F!"
echo del "%%a"
)


=====================================
Helping others achieve escape felicity

M2


Report •

#3
December 2, 2009 at 14:46:51
Any particular reason you're copying & deleting, instead of
renaming?

Report •

Related Solutions

#4
December 2, 2009 at 15:48:53
klint,

To avoid a name collision.


=====================================
Helping others achieve escape felicity

M2


Report •

#5
December 2, 2009 at 18:34:35
I see. You could also do move /y oldname newname, which I
think will be more efficient than copying especially if it's a large
file.

Report •


Ask Question