Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch - for loop  (Read 11322 times)

0 Members and 1 Guest are viewing this topic.

DaveTheRave

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Unknown
    Batch - for loop
    « on: April 16, 2011, 04:20:54 PM »
    Hello, I need some assistance, the web or the help does not seem to work or I'm not understanding it.

    I'd like to create a batch file that will read in a list of IP addresses and for each IP address perform a ping and then a traceroute. 

    To reduce iteration I figure I have an initial batch file, a text file with all the IP address (which could expand) and then a 2nd batch file that will run the ping and traceroute from the list (again other commands or tasks may well take place within this)

    Initial.bat
    FOR /f "eol=" %%L IN (c:\list.txt) DO call c:\2nd.bat  %%L

    2nd.bat
    ping %%L
    tracert %%L

    The black windows appears for a split second, and nothing happens.  I have tried putting in pause statements, and replaced the call with echo, do see what is happening, even tried sending the output to another text file.  Nothing seems to work.  Even tried using an example from the cmd prompt "for /?"

    for /F "eol=; token=2,3* delims=, " %i in (c:\list.txt) do @echo %i %j %k

    Could somebody advise  ??? :D ???


    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Batch - for loop
    « Reply #1 on: April 16, 2011, 05:25:29 PM »
    Is this just to see if it can be done?
    Does it have a real world application?
    The reason I say that is many people come here already sure they need a batch file and have a hard time understanding the FOR command. If you are a professional programmer, and you do not understand the FOR command already, you may need to re think your objective. Peal, Python  or VB script are choices.
    My point is, are you sure you want to do this in batch?
    I can been done, it does not have to be a compact four line FOR loop that washes hides and puts them away.. The problem can be split into parts and each part tested by itself. Which is a standard practice anyway, unless the objective is to get a cryptic bit of code that you will not understand 30 days from now.

    DaveTheRave

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Unknown
      Re: Batch - for loop
      « Reply #2 on: April 16, 2011, 06:41:45 PM »
      hello,

      yes to see if it can be done and also how to understand how it is done. 
      depending on the success yes.

      Pro programmer no, I dabbled in it a few years back, i kinda understand the principles, I lack the syntax.  I think Pearl, Python, VB etc are making things to busy and complex.

      to reduce iteration I figured it would be easier and the general essence of programming to split things up and call functions, other programs and such.

      if it works, then to add more IP address to a list would be easier than adding the IP address and also the ping and tracert each time.  again should i wish to remove the ping this is a simple quick change that will happend only once.

      I figured this for loop would be ideal, and a batch file is simple and easy to transport  is this not the case??

      oldun

      • Guest
      Re: Batch - for loop
      « Reply #3 on: April 16, 2011, 07:03:47 PM »
      In 2nd.bat, change %%L to %1

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Batch - for loop
      « Reply #4 on: April 16, 2011, 07:28:42 PM »
      Can you provide a list of address that you want to ping?
      Is the purpose of this to make a report on the up time or performance of serves in different areas? If so, there already are thousands of free programs that do that.
      Myself, I avoid the use of FOR in a batch unless that is the best solution. When one has a desperate list, a list where the items differ in a large way, iteration may not be appropriate.

      Here is a disparate list of web sites in a batch file that calls another batch. The objective is to build a report using PING. We initialize the list on the first line with the > redirection symbol
      Code: [Select]
      REM BigList.bat
      echo Ping Test results > results.txt
      call pingtest.bat yahoo.com
      call pingtest.bat amor.com
      call pingtest.bat bearclw.com
      call pingtest.bat why-me.net
      ...
      echo ...all done!

      This big list calls a small batch that has more detail

      Code: [Select]
      REM pingtest.bat
      ping -a %1 >> results.txt

      The >> adds more to the text file.
      Later, use some other kind of program to compile the results.txt file into a report.

      The list was make with notepad or a similar editor that lets you insert a sedative string in front of a desperate list. Even a very large list will take less than a few moments to edit notepad.
      The advantage of this is that you can put in an error trap in the subordinate batch file that will stop and report an error. So you can go back and remove invalid items in the main file.  By insertion of a REM you can block a line.

      This kind of job is easier to maintain and modify than a FOR loop in simple cases. FOR is more suitable for manipulation of file names and attributes.

      In general, this Big List method is very simple to understand.
      Does this help?

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Batch - for loop
      « Reply #5 on: April 16, 2011, 07:29:45 PM »
      server error

      Salmon Trout

      • Guest
      Re: Batch - for loop
      « Reply #6 on: April 17, 2011, 12:39:16 AM »
      Before I start, here is a handy tip. When you are trying out a batch script, don't start it from Windows Explorer (by doubling clicking its icon). If you do this any errors can, and successfully reaching the end of the script will make the command window abruptly vanish. You can trap the script termination with a pause command, but if there is a prior error the window may vanish too quickly for you to read the error message.

      So run the script from a command window by typing its name and any parameter(s) at the prompt, this is easiest if you are in the same folder then you can just type its name and optionally its extension. The long winded way to get there is to type "cmd" in the Run box from the Start menu, and then navigate to the folder using CD. The quicker way is to use "Command Prompt here" from a right click on the folder in Windows Explorer. This is built in to Vista and later, with earlier Windows versions see here

      http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm

      Running the script this way means that when it terminates either at the end or because of a syntax error, or doesn't do what you expect, you get the prompt back and in the case of an error you see the script output and any error messages and this is available for you to look at, and in the event of mystification, to copy and paste it into a search engine or as part of a post in a help forum such as this one.

      Now to the matter in hand:

      You don't need 2 batch files.

      The general syntax of FOR is

      On one line:

      FOR ["options"] %%variable in (dataset) DO command

      Using parentheses you can make it span multiple command lines (many people indent the interior for readability):

      FOR ["options"] %%variable in (dataset) DO (
          command1
          command2
          command3
          etc
          )

      ["options"] may not always be present. Can contain skip= eol= tokens= delims=  sections.
      variable is a single letter from A-Z a-z.
      dataset can one or more files, wild cards can be used.

      To apply command to every line of a file you would use the /F switch

      Thus

      FOR /F %%A in (filename.txt) DO ping %%A

      or

      Code: [Select]
      @echo off
      for /f %%A in (ip-addresses.txt) do (
      ping %%A
      tracert %%A
      )
      echo.
      echo *** loop completed ***
      echo.
      pause

      Here is a text file (It has 2 lines but could have more). I have called it ip-addresses.txt

      Code: [Select]
      87.248.112.81
      209.85.229.99

      Running the above batch code produces this output at my location:
      Code: [Select]
      Pinging 87.248.112.81 with 32 bytes of data:
      Reply from 87.248.112.81: bytes=32 time=41ms TTL=53
      Reply from 87.248.112.81: bytes=32 time=51ms TTL=52
      Reply from 87.248.112.81: bytes=32 time=40ms TTL=53
      Reply from 87.248.112.81: bytes=32 time=44ms TTL=53

      Ping statistics for 87.248.112.81:
          Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
      Approximate round trip times in milli-seconds:
          Minimum = 40ms, Maximum = 51ms, Average = 44ms

      Tracing route to fe15.global.sports.ird.yahoo.com [87.248.112.81]
      over a maximum of 30 hops:

        1     8 ms     7 ms     7 ms  10.15.176.1
        2     7 ms     7 ms     7 ms  aztw-geam-1b-ge212.network.virginmedia.net [80.1.243.137]
        3     7 ms     7 ms     7 ms  aztw-core-1b-ae0-0.network.virginmedia.net [80.1.241.13]
        4    13 ms    10 ms     9 ms  brhm-bb-1b-as1-0.network.virginmedia.net [213.105.175.161]
        5    13 ms    13 ms    13 ms  nrth-bb-1a-as4-0.network.virginmedia.net [62.253.185.105]
        6    11 ms    11 ms    11 ms  nrth-tmr-1-ae1-0.network.virginmedia.net [213.105.159.30]
        7    31 ms    31 ms    33 ms  fran-ic-1-as0-0.network.virginmedia.net [62.253.185.81]
        8    27 ms    27 ms    64 ms  ge-1-3-0.pat1.dee.yahoo.com [80.81.192.115]
        9    26 ms    27 ms    29 ms  so-2-0-0.pat1.ams.yahoo.com [66.196.65.144]
       10    44 ms    45 ms    43 ms  UNKNOWN-66-196-65-X.yahoo.com [66.196.65.81]
       11    44 ms    52 ms    43 ms  ae-1.msr1.ird.yahoo.com [66.196.67.231]
       12    47 ms    46 ms    79 ms  te-7-4.bas-b1.ird.yahoo.com [87.248.101.103]
       13    41 ms    52 ms    53 ms  fe15.global.sports.ird.yahoo.com [87.248.112.81]

      Trace complete.

      Pinging 209.85.229.99 with 32 bytes of data:
      Reply from 209.85.229.99: bytes=32 time=25ms TTL=53
      Reply from 209.85.229.99: bytes=32 time=25ms TTL=53
      Reply from 209.85.229.99: bytes=32 time=25ms TTL=52
      Reply from 209.85.229.99: bytes=32 time=26ms TTL=52

      Ping statistics for 209.85.229.99:
          Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
      Approximate round trip times in milli-seconds:
          Minimum = 25ms, Maximum = 26ms, Average = 25ms

      Tracing route to ww-in-f99.1e100.net [209.85.229.99]
      over a maximum of 30 hops:

        1     6 ms     7 ms     7 ms  10.15.176.1
        2     8 ms     7 ms     8 ms  aztw-geam-1b-ge136.network.virginmedia.net [80.1.242.121]
        3     7 ms     7 ms     7 ms  aztw-core-1b-ae0-0.network.virginmedia.net [80.1.241.13]
        4     9 ms     9 ms     9 ms  brhm-bb-1b-as1-0.network.virginmedia.net [213.105.175.161]
        5    11 ms    11 ms    11 ms  manc-bb-1a-as6-0.network.virginmedia.net [62.253.185.134]
        6    12 ms    13 ms    17 ms  manc-bb-1b-ae0-0.network.virginmedia.net [62.253.187.178]
        7    78 ms    22 ms    19 ms  tele-ic-3-ae0-0.network.virginmedia.net [212.43.163.70]
        8    21 ms    19 ms    19 ms  138-14-250-212.static.virginmedia.com [212.250.14.138]
        9    20 ms    20 ms    19 ms  209.85.252.76
       10    26 ms    25 ms    25 ms  72.14.232.134
       11    72 ms    26 ms    28 ms  216.239.49.45
       12    25 ms    33 ms    29 ms  209.85.243.85
       13    27 ms    25 ms    20 ms  ww-in-f99.1e100.net [209.85.229.99]

      Trace complete.

      *** loop completed ***

      Press any key to continue . . .

      You don't say why you are using "eol=" in the options block. If it is not used, you can comment out lines in the text file simply with a semicolon.

      Thus in this text file, using my above syntax, the third line will be ignored:

      Code: [Select]
      87.248.112.81
      209.85.229.99
      ;212.58.224.138
      212.250.162.12

      By the way, this is an ip address:

      Code: [Select]
      209.85.229.99
      and this is (part of) a url:

      Code: [Select]
      www.yahoo.com












      « Last Edit: April 17, 2011, 01:13:12 AM by Salmon Trout »

      Salmon Trout

      • Guest
      Re: Batch - for loop
      « Reply #7 on: April 17, 2011, 02:24:26 AM »
      ..

      DaveTheRave

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Unknown
        Re: Batch - for loop
        « Reply #8 on: April 18, 2011, 10:01:43 AM »
        Kudos Salmon Trout  ;D

        worked like a treat.  For confirmation and historical purposes I have done the following.

             Initial Batch file....
        FOR /F %%A in (list.txt) DO call bat2.bat %%A

             list.txt
        Just a simple list of non sequenced IP address's

              called batch file
        ping %1
        tracert %1


        This will read in, a line at a time a list of IP address's and perform a ping and a tracert to that particular IP address.

        Excellent thanks again  :)