How to read a line using "!" from file in windows script package?

I am writing a script package to read from a file. The file contains lines such as token = value. I have code to analyze each line of the file, and it is stored in %% i. The following code tries to extract the value of the token:

Please note that this script uses a delayed extension as indicated in the comments.

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

If the password value with a token contains "!" then "!" skipped, and only the rest of the line is stored in a variable password. Example if line:

password = Test!

then the password = Test variable. I tried changing the input file in various ways, and the script package reads everything except "!" . I used:

  • password = "Test!"
  • password = "Test ^!"
  • password = Test%!

    password = Test%!

"!" . , "!" ?

+4
2

, , . ! , :

@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

password setlocal, .

+5

, . , , %% i.

%% a, %% . , %% .

CMD , 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%

:

i: password=Test!
password: Test!

, , Test! for , %% password=Test!. , .

+1

Source: https://habr.com/ru/post/1609687/


All Articles