0

Example:

:loop
set n=0
set m=254
set /a n+=1
ping -n 1 -w 500 xxx.xxx.xxx.%n%  | find /i "reply" > file

=====BELOW is what I need=====
set a=0
set /a a+=1
set %a%= < file
====ABOVE is what I need=====

if %n% lss %m% goto loop

So specifically I need a batch script that can make number of variables as much as he loops. I searched a lot for answer and even tried few ideas on my own... but I can't figure this out... I guess lack of batch knowledge since I am ubuntu user and not Win. Thanks in advance. Regards

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • tell please what you want to do in "set %a%= < file" ? – mihai_mandis Dec 20 '13 at 18:24
  • in bash you can use for loop like this: for ((i=0; i<$m; i++)) do var$i = "foo_$i" done – ptitpion Dec 20 '13 at 18:57
  • 1
    I suggest you to read [this post](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) about arrays in Batch files – Aacini Dec 21 '13 at 00:48

3 Answers3

3

Use FOR /L instead of SET /A with GOTO

Use FOR /F to process results of command instead of temp file and SET /P

Use carefully constructed variable names to emulate an array (potentially sparse array in this case)

for /l %%N in (1 1 255) do (
  for /f "delims=" %%A in (
    'ping -n 1 -w 500 xxx.xxx.xxx.%%N^|find /i "reply"'
  ) do set "addr[%%N]=%%A"
)
dbenham
  • 127,446
  • 28
  • 251
  • 390
1
@echo off
    setlocal enableextensions disabledelayedexpansion

    for /l %%n in (1 1 254) do (
        for /f "tokens=*" %%r in (
            'ping -n 1 -w 500 xxx.xxx.xxx.%%n ^| find "reply" '
        ) do set "a[%%n]=%%r"
    )
    set a[

    endlocal

This uses for /f to run a command (ping | find) and assing the output of the command (1 line of the ping response, filtered by find) to a variable with a incrementing number in its name.

set a[ is used to show the resulting data on console

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thanks for help, well your answers didn't quiet gave whole answer to my problem since its still has some bugs, but it gave quiet big piece of puzzle and now from where to start my research and from where to start learning to solve this problem. So I appreciate your help very much :) – user3123711 Dec 20 '13 at 18:49
  • @user3123711: What bugs? – MC ND Dec 20 '13 at 18:56
0
@echo off
if exist Pingresult.txt del Pingresult.txt 

for /l %%n in (1 1 254) do ping -n 1 -w 500 xxx.xxx.xx.%%n | find /i "reply" >>PingResult.txt
SachaDee
  • 9,245
  • 3
  • 23
  • 33