Find and replace with reserved characters.
Posted: 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.
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
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