Batch file to read every X line from a file

August 3, 2011 at 11:43:25
Specs: Windows XP
I have a Batch file that is as follows:

for /f %%A in (ids.txt) do (
@echo %%A
systeminfo /s %%A >> Info.txt)

What I want to do is have this same command in 4 different batch files and have the first one read line 1, then 5, then 9, etc. The second one read line 2, then 6, then 10, etc.

Just not sure how to start at something other than number 1 and then how to skip the ones in between.


See More: Batch file to read every X line from a file

Report •

#1
August 3, 2011 at 12:28:40
You can use the "GOTO" command. Take a look at this example:

@echo off
:loop
set num=%random%
if /i %num% GTR 5 GOTO loop
if /i %num% LSS 1 GOTO loop
GOTO finish
:finish
ECHO The random number generated was %num%.
PAUSE

You can have the batch file after the first line of code is executed GOTO 5
Line 1's code here
GOTO 5
Line 2
Line 3
Line 4
:5
Line 5's code here.

Ask and Answer. The way of learning.
But first ask the right way.


Report •

#2
August 3, 2011 at 12:46:22
Hmm, not sure what you mean.

What I have is a huge text file with a list of IDs. I just need a batch file to read through the list and run the systeminfo command on that ID. Then I need it to skip a certain number and read in the next one.

So my output would be:
Systeminfo ID1
Systeminfo ID5
Systeminfo ID9

When I read in from a file, is there a counter somewhere that increments s it reads the data so I can tell which record it's on? If so, I can just do some math and figure out which one sit should process and which ones it shouldn't


Report •

#3
August 3, 2011 at 14:09:05
Okay, now I know what you mean. I don't know very much about how to do that though sadly :(.

Ask and Answer. The way of learning.
But first ask the right way.


Report •

Related Solutions

#4
August 3, 2011 at 14:09:45
Actually maybe look at this thread:
http://www.computing.net/answers/pr...

Ask and Answer. The way of learning.
But first ask the right way.


Report •

#5
August 3, 2011 at 14:25:57
Your initial post got me thinking of a different way.
I just set a variable before the loop and increment it as the loop goes through. IF it's a 1 I run the command. If it's a 2, 3, or 4 I don't. When it's a 4, I set it to 0 so the next time through it's a 1 and I run the command.

On my other files I just change the start value of the variable so they all end up picking up different items from the list.

Here's what I ended up with:

@echo off
setlocal EnableDelayedExpansion
set /a cnt=0

for /f %%A in (ids.txt) do  (
     
 set /a cnt=!cnt!+1
     
  if !cnt! == 1 (
         echo %%A
         systeminfo /s %%A  /fo table /nh > %%A.csv
       )
     if !cnt! == 4 (set /a cnt = 0)
  )


Report •

Ask Question