markus625
markus625

Reputation: 33

FOR /F is this the correct method for skipping a line in a batch file

I am trying to find and use a specific word as a variable to print into a txt file using a batch script.

So this is an example of the text.

10-03-2021 18:46:12:77 INFO: [SET_APP_VARS] Country:UK
10-03-2021 18:46:12:78 INFO: [SET_APP_VARS] Langauge:ENGLISH
10-03-2021 18:46:12:78 INFO: [SET_APP_VARS] Type:CLASSIC
10-03-2021 18:46:12:78 INFO: [SET_APP_VARS] Lane:801

So I need to find the word CLASSIC and use it as a variable

Is the below code correct.

FOR /F "skip=2 tokens=1,2,3,4,5,6 delims=:" %%a IN (test.txt) do echo  Country is "%%f" & goto next

:next

FOR /F "skip=4 tokens=1,2,3,4,5,6 delims=:" %%a IN (test.txt) do echo  Motherboard is is "%%f" & goto done

:done

Upvotes: 2

Views: 3801

Answers (3)

markus625
markus625

Reputation: 33

Hi thanks guys for the input, before I read the replies here I had actually came up with something different because the log file was encoded in UCS-2-LE so the code wouldn't read the file so I changed it to UTF with the command below.

powershell -command "Get-Content C:\Install\Logs\*LOAD-BOOTS_ENVIRONMENT.log" > C:\Install\Logs\Boots_logfile.txt 

Also the log file starts with a date/time appended to the start of the file name like 20210311215550LOAD-BOOTS_ENVIRONMENT so I convert this to UTF with the name Boots_logfile.txt.

So in this file is a line of text like below (line numbers not always the same)

11-03-2021 21:56:13:58 INFO: [SET_STORE_NUMBER] Name:Manchester
11-03-2021 21:57:02:71 INFO: [SET_APP_VARS] SET Country:UK
11-03-2021 21:57:02:74 INFO: [SET_APP_VARS] SET Language:ENGLISH
11-03-2021 21:57:02:81 INFO: [SET_APP_VARS] Key:STORENUMBER Value:120
11-03-2021 21:57:02:89 INFO: [SET_APP_VARS] Key:IPADDRESS Value:10.10.10.10

So i wrote the code as below to find the store name Manchester

set torun=findstr /c:"[SET_STORE_NUMBER] Name" "C:\Install\Logs\Boots_logfile.txt" 
for /f "tokens=1,2,3,4,5,6 delims=:" %%a in ('%torun%') do set boots001="%%f"

And use echo Store Name%TAB%%TAB%%boots001% to display it

Does this look about right or is there a shorter method, so as above I would get the store name,number ip address etc etc with %boots002% %boots003% etc

Upvotes: 0

Stephan
Stephan

Reputation: 56180

Filtering your input helps:

for /f "tokens=6 delims=:" %%a in ('find " Country:" test.txt') do echo Country is %%a
for /f "tokens=6 delims=:" %%a in ('find " Type:" test.txt') do echo Type is %%a

or if you need more:

    for /f "tokens=8,9 delims=: " %%a in (test.txt) do echo %%a is %%b

or using variables:

for /f "tokens=8,9 delims=: " %%a in (test.txt) do set "_%%a=%%b"
set _

or just some (much faster than the first snippet, because it has to read test.txt just once):

for /f "tokens=8,9 delims=: " %%a in ('findstr "Country: Type:" test.txt') do set "_%%a=%%b"
set _

Upvotes: 3

Compo
Compo

Reputation: 38623

Based upon what I think you may be doing, I would perhaps offer a different methodology:

@Echo Off
SetLocal EnableExtensions
For /F "UseBackQ Tokens=1,* Delims=]" %%G In ("test.txt") Do (
    For /F "Tokens=1,* Delims=: " %%I In ("%%H") Do Echo Set "%%I=%%J" 
)
If "%Type%" == "CLASSIC" …

Just replace in the last line with the rest of your intended command(s), as needed.

Upvotes: 1

Related Questions