Solved Setlocal command preventing exclamations from being replaced

August 16, 2014 at 11:28:59
Specs: Windows 64
The below code works fine to replace exclamations found in any filename and replaces them with an "a", however I have a big batch where this code is included and the problem is if the words:
"setlocal ENABLEDELAYEDEXPANSION" are used anywhere in the batch before this code, the code doesnt replace the exclamation marks and instead says :
" =a% was not expected at this time"

What do I type to turn off the
"setlocal ENABLEDELAYEDEXPANSION"
command when the batch goes to this code and why does this happen?I use the setlocal command to set different lines as variables in a text. For some reason it only happens with exclamations and not with end brackets or anyother characters I want to replace. Thanks.

FOR %%a IN (*.*) DO (
SET Var=%%~na
CALL SET Var=%%Var:!=a%%
CALL REN "%%a" "%%Var%%.*"
)


See More: Setlocal command preventing exclamations from being replaced

Report •


#1
August 16, 2014 at 13:59:35
Yep, you've encountered another gotcha with batch. The only workaround I know of is to not use delayed expansion.

How To Ask Questions The Smart Way


Report •

#2
August 16, 2014 at 20:02:59
✔ Best Answer
This worked for my tests:

FOR %%a IN (*.*) DO (
SET Var=%%~na
call :x
CALL REN "%%a" "%%Var%%.*"
)
goto :eof

:x
echo incoming: %var%
set var=%var:!=***%
echo returning: %var%

message edited by nbrane


Report •

Related Solutions


Ask Question