Search code examples
windowsbatch-fileescaping

Batch file with argument with escape character ^


I'm trying to call the batch script with password looking like:

ABC^=abc

Unfortunately, all my tries finish with result of:

ABC=abc

The "^" character is omitted and I don't know how to force in script to read argument with this char.

I have the line of code (where PASS is localization of file with passwords):

:get_pass
    set PASS=%~1
    set USR=%~2
    set passwd=

    for /f "tokens=1,2 delims= " %%a in (%PASS%) do (
      if %%a==%USR% (
        set "passwd=%%b"
      )
    )

And I call this func in command line:

    call script.cmd get_pass C:\Doc\pass.ini USR do set passwd=%%i
    echo %passwd%
    -> ABC=abc

The result is always without ^ char. I've tried a different combination of double quotes around argument like: "passwd=%%b", passwd="%%b", but it didn't work.

I'll be very grateful for your help!

Thanks, Kamil


Solution

  • The correct value is already stored in the variable, but your echo %passwd% is the problem.

    You could check the value by

    set passwd
    

    And use the value with delayed expansion instead

    setlocal EnableDelayedExpansion
    echo !passwd!