4

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:

  1. password="Test!"

  2. password="Test^!"

  3. password=Test%!

    password=Test%!

and everything skips "!". Any idea how I can read a string with "!" into a variable?

5
  • 1
    Maybe ^^! could work?
    – aschipfl
    Commented Sep 30, 2015 at 19:07
  • Tried that too. Input Test^^! output Test^
    – cppcoder
    Commented Sep 30, 2015 at 19:21
  • 1
    What about ^^^!? (I'm being completely serious, by the way.) Commented Oct 1, 2015 at 2:11
  • 2
    The ! 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.
    – dbenham
    Commented Oct 1, 2015 at 4:48
  • Thanks, @dbenham. I can verified that ^^^! works with the test script on my answer when expansion is enabled. Commented Oct 1, 2015 at 12:35

2 Answers 2

5

Assuming you're using delayed variable expansion just disable it temporarily for the comparison. To compare in the non-delayed mode I'd parse the current line splitting by ! and check if the first token is the same as the entire line:

@echo off
setlocal enableDelayedExpansion
for /f "delims=" %%i in (sourcefile.txt) do (
    setlocal disableDelayedExpansion
    for /f "delims=! tokens=1" %%z in ("%%i") do (
        if "%%z"=="%%i" ( rem The line has no !
            for /f "tokens=1* delims==" %%a in ("%%i") do (
                if "%%a"=="password" ( set "password=%%b" )
            )
        )
    )
    endlocal
)
pause

However password variable will be only available in the inner setlocal context, to export it use that answer.

4
  • Thanks for the reply. I do have "setlocal EnableDelayedExpansion". If I simply set it to "setlocal disableDelayedExpansion" , I'm able to parse the strings with "!". Any idea what will be the effect if I just disable Delayed expansion.
    – cppcoder
    Commented Sep 30, 2015 at 20:30
  • @cppcoder, when it's disabled variables like %password% inside the loop will be expanded just one time before the entire loop is parsed and executed so it'll be empty in your case. See also Enable and Disable Delayed Expansion, what does it do?
    – woxxom
    Commented Sep 30, 2015 at 20:59
  • But after the endlocal the password variable will gone. To avoid this you need a safe return technic over the endlocal barrier
    – jeb
    Commented Oct 1, 2015 at 8:09
  • Yep, that's what I was talking about in the post. Thanks for the link, added to the answer.
    – woxxom
    Commented Oct 1, 2015 at 8:13
1

I am not experiencing that problem with the code you posted. I think the error may precede the code you posted which stores each line of the file in %%i.

Put an echo statement in there before your %%a loop so that you can verify the contents of %%i before processing further. I highly suspect that the exclamation point isn't making it into %%i in the first place.

Here is the full CMD file I ran on Windows 7:

@echo off
for /f "tokens=1* delims= " %%i in ("password=Test!") do (

echo i: %%i
   for /f "tokens=1* delims==" %%a in ("%%i") do (  
      if "%%a"=="password" ( set password=%%b )  
   )
)
echo password: %password%

The output is as follows:

i: password=Test!
password: Test!

So you can see that Test! is making it through the for loop intact when %%i contains password=Test!. If you want to post the earlier pieces of your batch file, we can help you troubleshoot further.

5
  • I have the same issue. Following code is used to read line by line from file stored in configFile: for /f "tokens=1* delims= " %%i in (%configFile%) do ( code above to tokenize the line ) . "%%i" does not have "!". Is there a way to extract the "!" in a line
    – cppcoder
    Commented Sep 30, 2015 at 19:55
  • Mine still works with that line added as well. I think we are missing some information. Did you happen to use setlocal EnableDelayedExpansion ? That will alter the behavior of parsing exclamation points within the variables. Your code works, unless I enable delayed expansion. With expansion turned off, it works, but you won't be able to use the variable password until you are safely outside of the for loops, and there can only be one password per file since it only keeps the last one. You might need to post your whole script rather than the fragments we have so far. Commented Sep 30, 2015 at 21:31
  • The op mentioned that he uses delayed expansion, that's the cause why it works in your example without delayed expansion
    – jeb
    Commented Oct 1, 2015 at 8:10
  • I missed that update before my previous comment. I'm afraid that the only way to get that working is for the exclamation point to be escaped in the source file, which is probably going to require formatting the file before running it through this script. Perhaps splitting this up into two batch files would help, one being CALLed from another. Commented Oct 1, 2015 at 11:54
  • OK, so now that we have all the facts gathered together, some better answers than this one are coming forth. Awesome! Commented Oct 1, 2015 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.