In need of help doing a FOR /F Do loop in a CMD Batch


  1. Posts : 5
    Windows 7 Ultimate x64
       #1

    In need of help doing a FOR /F Do loop in a CMD Batch


    I am in need of help doing a FOR /F Do loop in a CMD Batch script.

    I am trying to write a batch script that will ping all IP addresses in a host file except for an IP address that starts with 127 and skip all commented lines in the hostfile or that start with a #.

    my current code will ping the first word/string in every uncommented line, so I am halfway there. However I can't figure out how to compare the first 4 characters in the word/sting to 127. and have it skip it if starts with those characters.

    Here is my working code. Any help will be appreciated.

    Code:
    @ECHO off
    setlocal EnableExtensions EnableDelayedExpansion
    
    DEL C:\temp\Pings.txt
    
    ECHO. > C:\temp\Pings.txt
    
    For /F "eol=# tokens=1" %%A in (c:\temp\TESThost) do ECHO PING -a "%%A" && ECHO. >> C:\temp\Pings.txt && ECHO. >> C:\temp\Pings.txt && ECHO PING -a "%%A" >> C:\temp\Pings.txt && PING -a "%%A" >> C:\temp\Pings.txt && ECHO ---------------------------------------------------------------- >> C:\temp\Pings.txt
      My Computer


  2. Posts : 450
    Windows 7
       #2

    Do you mean 3 characters or 4?

    /* first, cast the %% variable to a non %% so we can work with it */
    SET var = %%A
    /* parse relative bytes 0 thru 2 into var2 */
    SET var2 = !var:~0,2!
    /* compare 1st 3 bytes to number 127
    IF !var2! == 127 (
    insert more code here
    )
      My Computer


  3. Posts : 5
    Windows 7 Ultimate x64
    Thread Starter
       #3

    So you are suggesting something like this?

    Code:
    @ECHO off
    setlocal EnableExtensions EnableDelayedExpansion
     
    DEL C:\temp\Pings.txt
     
    ECHO. > C:\temp\Pings.txt
     
    For /F "eol=# tokens=1" %%A in (c:\temp\TESThost) do (
    SET var = %%A
    SET var2 = !var:~0,2!
    IF !var2! == 127 (
    ECHO PING -a "%%A" && ECHO. >> C:\temp\Pings.txt && ECHO. >> C:\temp\Pings.txt && ECHO PING -a "%%A" >> C:\temp\Pings.txt && PING -a "%%A" >> C:\temp\Pings.txt && ECHO ---------------------------------------------------------------- >> C:\temp\Pings.txt))
      My Computer


  4. Posts : 5
    Windows 7 Ultimate x64
    Thread Starter
       #4

    This does not seem to work when I run this cmd file at the cmd prompt

    Code:
    @ECHO off
    setlocal EnableExtensions EnableDelayedExpansion
    DEL C:\temp\Pings.txt
    ECHO. > C:\temp\Pings.txt
    For /F "eol=# tokens=1" %%A in (c:\temp\TESThost) do (
      Set var=%%A
      echo %%A
      echo %var%
      )
    I get this output

    Code:
     
    I get this output
    C:\temp>Test.cmd
    127.0.0.1
    ECHO is off.
    192.168.0.1
    ECHO is off.
    192.168.0.2
    ECHO is off.
    C:\temp>
    Best I can tell it is not setting the variable correctly because when it is suppsoed to be echoing the value of %var% it echos empty.
      My Computer


  5. Posts : 450
    Windows 7
       #5

    I assume the 127. and 192. output echo lines above are from the echo %%A statement. So, %%A is getting loaded/set.

    What if you change:

    echo %var%

    to

    echo !var!
      My Computer


  6. Posts : 5
    Windows 7 Ultimate x64
    Thread Starter
       #6

    that seems to work

    Using this code

    Code:
    @ECHO off
    setlocal EnableExtensions EnableDelayedExpansion
    DEL C:\temp\Pings.txt
    ECHO. > C:\temp\Pings.txt
    For /F "eol=# tokens=1" %%A in (c:\temp\TESThost) do (
      Set var=%%A
      echo %%A
      echo !var!
      )
    I get this result

    Code:
    C:\temp>temp.cmd
    127.0.0.1
    127.0.0.1
    192.168.0.1
    192.168.0.1
    192.168.0.2
    192.168.0.2
      My Computer


  7. Posts : 5
    Windows 7 Ultimate x64
    Thread Starter
       #7

    Final code that does what I want it do do turns out like

    Code:
    @ECHO off
    setlocal EnableExtensions EnableDelayedExpansion
    DEL C:\temp\Pings.txt
    ECHO. > C:\temp\Pings.txt
    For /F "eol=# tokens=1" %%A in (c:\temp\TESThost) do (
      Set var=%%A
      set var2=!var:~0,3!
      IF NOT !var2! == 127 (ECHO PING -a "%%A" && ECHO. >> C:\temp\Pings.txt && ECHO. >> C:\temp\Pings.txt && ECHO PING -a "%%A" >> C:\temp\Pings.txt && PING -a "%%A" >> C:\temp\Pings.txt && ECHO ---------------------------------------------------------------- >> C:\temp\Pings.txt)
      )
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 18:52.
Find Us