Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Aug 2014
    Posts
    123
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Batch for commands

    I am suddenly having problems with FOR command (I think).
    I have tried with/without SETLOCAL ENABLEDELAYEDEXPANSION and get similar or identical results: echoing of tokens with:
    echo D=%%D ends up as D=%D. Tim & dat show the command not the result. Here is the code:
    Code:
    set tmp=C:\Users\myuser\Desktop\xxxx\tmp
    REM For every IP address in ipList
    FOR /F %%A IN (C:\Users\myuser\Desktop\xxxx\ipList.txt) DO (
    REM Loop 8 times with 15 second separation
       FOR /L %%M IN (1, 1, 8) DO (
    time /T
          set tim=time /T
          set dat=date /T
          
    ECHO TEST TIM=%tim% DAT=%dat%
          ping -n 4 -4 %%A > %tmp%
    REM Get packets dropped
          FOR /F "skip=7 tokens=10 delims= " %%C IN (%tmp%) DO (
             set lost=%%L
    echo tmp=%tmp% C=%%C D=%%D E=%%E F=%%F G=%G% H=%H%
    echo TEST lost= %lost% =%%L
    I have look at it so long I can't see my mistake. TNX

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,168
    Thanks
    47
    Thanked 978 Times in 908 Posts
    Problems
    You have changed the TMP variable. Windows sets this to a directory for you and you shouldn't change it, just use it as is and supply a file name.
    You haven't closed any of your brackets.
    You are trying to set values from commands that haven't run yet: set tim=time /T
    You have specified which token you want but then use the wrong variable: FOR /F "skip=7 tokens=10 delims= " %%C IN (%tmp%) DO ( set lost=%%L

    Questions
    I can't see how you manage the 15 second delay?
    Do you want to run the ping 8 times or run it once, then wait before doing the next IP?

    Things you can test
    Using a temporary file: set mytmp=%tmp%\tmpfile.txt
    Set a variable to the current time. Windows provides the current date and time as a variable: echo %time%
    Reading your ping output: for /f "tokens=10" %C in (" Packets: Sent = 4, Receive
    d = 0, Lost = 4 (100% loss),") do echo %c


    cheers, Paul

  3. #3
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,413
    Thanks
    33
    Thanked 195 Times in 175 Posts
    Well, there should be around three closing right brackets at the end of the file, for a start!
    You don't ever set the variables %%D, or %%E, or %%F...

    In the final FOR command, should your tokens=10 be something like tokens=7-10 ?

    I'm off to work so can't investigate further, or offer some "tidy-up" suggestions.
    (Other than that you don't actually need ENABLEDELAYEDEXPANSION on the SETLOCAL command!)
    BATcher

    Time prevents everything happening all at once...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •