Find and replace with reserved characters.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
farhankazi
Posts: 1
Joined: 27 Sep 2009 18:42

Find and replace with reserved characters.

#1 Post by farhankazi » 27 Sep 2009 19:04

Dear All,

I have following batch script which finds specified character in file names and replace it with another character that is mentioned inside the script.

Code: Select all

:: ----- Batch Script Start ------- ::
ECHO OFF
SETLOCAL EnableDelayedExpansion

SET searchIn=C:\eBooks
SET FindChr=%
SET ReplaceChr=_

FOR /F "delims=" %%f IN ('DIR /S /A-D /B "%searchIn%\*%FindChr%*.*%"') DO (
   SET var=
   SET var=%%~nf%%~xf
   SET var=!var:%FindChr%=%ReplaceChr%!
   ECHO "%%f" -^> "!var!"
   Ren "%%f" "!var!")
:: ----- Batch Script End ------- ::



It works perfectly with characters like -, @, #, $ etc... but problem arises when I specify reserved characters like %, !, =, ~ etc...

Can you please guide me how do I make above script generic so that any character can be used with FindChr and ReplaceChr variables.

Regards,
Farhan Kazi

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#2 Post by ghostmachine4 » 02 Oct 2009 09:22

you should be aware that those characters are forbidden if you want to use them in files or folder names.

sahmeepee
Posts: 1
Joined: 10 Oct 2009 17:47

#3 Post by sahmeepee » 10 Oct 2009 18:02

ghostmachine4 wrote:you should be aware that those characters are forbidden if you want to use them in files or folder names.


In what filesystem are % and ! illegal characters for filenames? Certainly not any filesystem I've come across.

I am also having difficulty replacing %. I'm looking to be able to replace a % in a variable which holds a filename with %%. If I leave the variable as-is, then the part of the script which uses it later treats the % as a special character, effectively stripping it from the filename and causing an error:

FINDSTR: Cannot open path\filewithapercentsigninitsname.txt

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 12 Oct 2009 10:16

First, if you're going to be doing DOS scripting, AND if you have control over filenames/directory names, then please avoid anything that could be considered a special character.

If you must deal with special characters, your best idea is to use something other than DOS. It's often just way too difficult to get it all to work -- especially with 1 giant "generic" script.

BUT, in general, you can escape special characters using ^

and you can add it in using substring substitution like this:


set "filename=%1"
echo %filename:!=^!%

Note -- there's an extra % in this line:

FOR /F "delims=" %%f IN ('DIR /S /A-D /B "%searchIn%\*%FindChr%*.*%"') DO (

Now, for the %, all you need to do is change this line like this:

SET FindChr=%%

For the "!" character, you can't use delayedexpansion. This works:

Code: Select all

@ECHO OFF 
::SETLOCAL EnableDelayedExpansion

SET searchIn=C:\tmp
SET FindChr=!
SET ReplaceChr=_


FOR /F "delims=" %%f IN ('DIR /A-D /S /B "%searchIn%\*%FindChr%*.*"') DO (
   SET "var=%%~nf%%~xf"
   call SET "var=%%var:%FindChr%=%ReplaceChr%%%"
   call ECHO "%%f" -- "%%var%%"
   call Ren "%%f" "%%var%%"
)
I couldn't get the -> part to translate properly so I changed it to --

For the ~ character, you'll have to change the for command like this:

FOR /F "delims=" %%f IN ('DIR /S /A-D /B "%searchIn%\*"^|find "~"') DO (

AND you'll have to add a ^. So far this is all I could get to work for a ~

Code: Select all

::@ECHO OFF 
::SETLOCAL EnableDelayedExpansion

SET searchIn=C:\tmp
SET FindChr=^~
SET ReplaceChr=_


FOR /F "delims=" %%f IN ('DIR /A-D /S /B "%searchIn%\"^|find "%findchr%"') DO (
   SET "var=%%~nf%%~xf"
   call SET "var=%%var:^%FindChr%=%ReplaceChr%%%"
   call ECHO "%%f" -- "%%var%%"
   call Ren "%%f" "%%var%%"
)


For the = character, I think the only way to do it is to go character by character in a loop something like this (completely untested):

Code: Select all

set "origfilename=test=test.txt"
set "oldfilename=%origfilename%"
set "findchr=="
set "replacechr=_"
:loop
if "%oldfilename:~0,1%"=="%findchr%" (
   set "newfilename=%newfilename%%replacechr%"
   ) else (
      set "newfilename=%newfilename%%oldfilename:~0,1%"
)
set "oldfilename=%oldfilename:~1,99999%"
if defined oldfilename goto :loop
ren "%origfilename%" "%newfilename%"

In fact, something like this last one might work for most special characters. The % will have to be doubled %%

Post Reply