Enrico Mangani
Enrico Mangani

Reputation: 87

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

Upvotes: 2

Views: 130

Answers (2)

Wasif
Wasif

Reputation: 15470

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.

Upvotes: 1

Hackoo
Hackoo

Reputation: 18827

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

Upvotes: 3

Related Questions