I'm writing a batch script to read from a file. The file contains lines such as token=value. I have code to parse each line of the file and it is stored in %%i. The following code tries to extract the value of token:
Take note that this script is using delayed expansion, as mentioned in the comments.
for /f "tokens=1* delims==" %%a in ("%%i") do (
if "%%a"=="password" ( set password=%%b )
)
If value of token password contains "!", then the "!" is skipped and only rest of the string is stored in variable password. Example, if the line is:
password=Test!
then variable password=Test . I have tried to change the input file various ways and batch script reads everything except "!". I have used:
password="Test!"
password="Test^!"
password=Test%!
password=Test%!
and everything skips "!". Any idea how I can read a string with "!" into a variable?
^^!
could work?^^^!
? (I'm being completely serious, by the way.)!
must pass through two FOR variable expansions (%%i
and%%a
) while delayed expansion is enabled. So to survive the two expansions, it must be escaped as^^^!
. But that is the value that would have to be in%%i
. You haven't shown how%%i
was populated - it is possible the source might require additional escaping, or not ;-) But escaping the source is probably not a good solution. You should show more of your code, and or describe your overall goal, if you want advice on how to proceed.^^^!
works with the test script on my answer when expansion is enabled.