Batch file to handle random % character in FOR LOOP
I am trying to pass file names as FOR loop parameters to a separate batch
file. The problem is, if a file name contains special characters
(especially %), the parameter doesnt go to the called script. EG -
The FIRST_SCRIPT.bat is as follows -
cd "C:\theFolder"
for /R %%a in (*.*) do call SECOND_SCRIPT "%%~a"
The SECOND_SCRIPT.bat is as follows -
ECHO %1
If a file name contains % eg. "% of STATS.txt", the output ends up being
of STATS.txt
Which is wrong. I have tried using Setlocal DisableDelayedExpansion but
with little success
Setlocal DisableDelayedExpansion
for /R %%a in (*.*) do (
SET "var=%%~a"
Setlocal EnableDelayedExpansion
call TEST_UPGRADE "%var%" "%%~a"
)
There are other stackoverflow answers, but they all need the % character
to be known before hand. Since the file names are not in our control,
these solutions won't work for us. Is there any way of handling this?
Thanks!
platform: cmd.exe for Windows XP
No comments:
Post a Comment