I have a script which uses SETLOCAL ENABLEDELAYEDEXPANSION
and takes user input. This is a problem if an input string has !
and then :
, as these characters and everything in between are removed.
Example: I enter a!b:c
with user input into x
and save it directly into the script as y
and z
.
SETLOCAL ENABLEDELAYEDEXPANSION
SET x=
SET /P x=
SET y=a!b:c
SET "z=a!b:c"
echo %x% %y% %z%
The result is ac ac ac
. How can I fix this for strings entered directly like y
and z
, and how can I fix it for user input like x
?
Your problem is not the input nor the delayed expansion mode.
But you should avoid percent expansion after you enabled delayed expansion.
Delayed expansion of variables is always safe, independent of the variable content.
set /p var=...
isn't a problem either.
setlocal DisableDelayedExpansion
set a=caret^^.
set "b=caret^"
SETLOCAL EnableDelayedExpansion
SET x=
SET /P x=
SET y=a^^!b:c
SET "z=a^!b:c"
echo !a! !b!
echo !x! !y! !z!