Sequence number in batch file

  • Thread starter Thread starter Wensi Peng
  • Start date Start date
W

Wensi Peng

Hello,

I want to add sequence number, 1, 2,3, .... in my output file to
identify/sort results by the number.
How can I do it in the batch files below? And how can I assign a line read
from screen after |findstr to a variable, so I
can use the value for the next command?

Thanks
Wensi

I want output like:
1, server1, reply
2, server2, reply
....
no, servern, reply.

Here are my tries:
My example 1: ( problem: how can I add the number)

for /f "tokens=1* delims=" %%i in (servers.txt) do (
echo %%i, >> output.txt
ping -n 1 %%i |findstr /i "reply from timed out unknown" >> output.txt
)

My example 2: (problem: how can I get the number and assign the value to a
variable)
set intNum = 0
for /f "tokens=1* delims=" %%i in (servers.txt) do (
echo intNum, %%i
ping -n 1 %%i |findstr /i "reply from timed out unknown"
set intNum = %intNum& +1
)
 
Wensi said:
I want output like:
1, server1, reply
2, server2, reply
....
no, servern, reply.
Try this, I'm unsure what you expect from the ping line.
The set /P avoids a crlf after output, requires w2k/XP.


@echo off&setlocal
set intNum = 1
for /f %%A in (servers.txt) do (
set /P dummy="%intNum%, %%A"<NUL
ping -n 1 %%A |findstr /i "reply from timed out unknown"
set /A intNum+=1
)

HTH
 
The number did not increase.
Ping line is just an example. I want to have a general batch template.
and use number and server name to identify/sort each line from output.

Thanks,
Wensi
 
Wensi said:
The number did not increase.
Ping line is just an example. I want to have a general batch template.
and use number and server name to identify/sort each line from output.
Looks like DelayedExpansion is needed ;-) Try this one :

::WensiPeng.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal EnableDelayedExpansion
set intNum=1
for /f %%A in (pclist.txt) do (
set /P dummy="!intNum!, %%A,"<NUL
ping -n 1 %%A|findstr /i "reply from timed out unknown"||echo.
set /A intNum+=1
)
::WensiPeng.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::

HTH
 
It works.

Thanks very much!
Wensi

Matthias Tacke said:
Looks like DelayedExpansion is needed ;-) Try this one :

::WensiPeng.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal EnableDelayedExpansion
set intNum=1
for /f %%A in (pclist.txt) do (
set /P dummy="!intNum!, %%A,"<NUL
ping -n 1 %%A|findstr /i "reply from timed out unknown"||echo.
set /A intNum+=1
)
::WensiPeng.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::

HTH
 
Back
Top