How to check the next line in a for loop

February 17, 2011 at 20:16:54
Specs: Windows 7
I have a file in the following format


For every line that starts with Machine, I want to check the next line in the file and if the next line contains a Machine as well, then i want to copy the original line and copy it to a file.

For eg.
Line 1 contains MachineA so i check Line 2. In this case Line 2 contains App1 so i would do nothing.
Line 4 contains MachineB so i check Line 5. In this case Line 5 contains MachineC so I would write the value of Line 4 (ie. MachineB) to an external text file.

I've tried playing around with for loops but haven't gotten too far

For /f "tokens=1 delims=[]" %%a in ('find /n "Machine" ^< example.txt') do (
set D=%%a
set F=D+1
for /f "tokens=1 delims=[] %%a in ('find /n /v "" ^< example.txt') do (

This is where i get lost.

Can anyone help?

See More: How to check the next line in a for loop

Report •

February 17, 2011 at 23:09:36
:: ==========================================
:: machine.bat Fri 18-02-2011 12:59:04.20
@echo off > newfile & setLocal enableDELAYedeXpansion

set P=X
for /f "tokens=* delims= " %%a in (somefile) do (
set S=%%a
if !P:~0^,7! equ Machine if !S:~0^,7! equ Machine (
>> newfile echo.!P!
set P=%%a

Life is too important to be taken seriously.


Report •

February 18, 2011 at 11:00:43
Brilliant. Thanks so much for your help M2

Report •

Related Solutions

Ask Question