简体   繁体   中英

Set into a variable a specific line from a file in Windows batch

I'm trying to get a line from a file in Windows batch with no success. I've tried several ways to do it with I'm not an expert and it's impossible to me. The number line to read is calculated dinamically:

set "file=C:\myfile.txt"

REM Looking for line number where the segment "<segment>" is.
for /f "tokens=1 delims=:" %%L in ('findstr /n "<segment>" %file%') do ( 
    set begin_line=%%L
)

echo %begin_line%

So I'm trying to read the line %begin_line%+1 and store it into a var. I'm sure that the way to achieve this is with for /f skip=%begin_line but I couldn't do it.

Any idea?

I hope what I have requested is possible.

Ivan

I'm not sure whether I understand your question. You are searching for the first line which contains the substring segment . Then you want to store the next line inside a variable. Is that correct? If yes, here is the code:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET inputFile=C:\myfile.txt
SET fount=0
FOR /F "tokens=*" %%F IN (%inputFile%) DO (
    IF !found!==1 (
        SET foundLine=%%F
        GOTO BREAK
    )
    SET line=%%F
    SET line=!line:^<segment^>=!
    IF NOT !line!==%%F SET found=1
)
:BREAK
ECHO !foundLine!
PAUSE

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