简体   繁体   中英

BATCH to get a specific string from a txt file

I would like to set a parameter that I can find in a txt file. This is a specific string, it's found in the first row after the character "=".

Example: description.txt

card=0123456789
status=false

I should take the string "0123456789" and set the parameter %str%. I tried this but it doesn't work:

for /f "tokens=2 delims= " %%a in ('type C:\tmp\description.txt^|find "card="') do (
set str=%%a & goto :continue
)
:continue
echo %str%

pause

how can I get the text after "=" in the first row?

TIA

//khs

You should set the delims like this "delims=="

@echo off
for /f "tokens=2 delims==" %%a in ('type "C:\tmp\description.txt" ^|findstr /bi "card="') do (
    set "str=%%a" & goto :continue
)
:continue
echo "%str%"
pause

Here is the code:

for /f "tokens=2 delims==" %%a in ('type C:\tmp\description.txt' | find "card"') do echo %%a

An extra delimiter = is definitely needed. Otherwise, it will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM