Reputation: 3
Im trying to write batch file code to check whether given line is present in text file or not. If it is present I want to get specific line(depending on line number) after that line into variable.. Can anyone help me?
For example
I have text file as...
EX1
EX2
Ex3
EX4
Ex5
now I want to search weather Ex3 is present in batch file or not. If it is present I want Ex5(2nd line after that) into variable.
Thanks in advance
Upvotes: 0
Views: 292
Reputation: 56586
This works on my box:
@echo off
SET searchterm=Ex3
SET /a lineafter=2
SET filename=lst.txt
:: --------------
SETLOCAL ENABLEDELAYEDEXPANSION
SET /a c=0
FOR /F "delims=" %%i IN (%filename%) DO (
if !c! GTR 0 (
IF %lineafter% EQU !c! (
SET result=%%i
GOTO :linefound
)
SET /a c=!c! + 1
) ELSE (
IF "%%i"=="%searchterm%" (
SET /a c=1
)
)
)
echo No result
GOTO :EOF
:linefound
echo Result: %result%
Just enter your values in the lines 2-4.
Upvotes: 1