Results 1 to 3 of 3
Thread: Batch for commands
2015-05-03, 19:38 #1
- Join Date
- Aug 2014
- 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:
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
2015-05-04, 01:15 #2
- Join Date
- Dec 2009
- Thanked 1,054 Times in 982 Posts
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
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
2015-05-04, 01:22 #3
- Join Date
- Feb 2008
- A cultural area in SW England
- Thanked 201 Times in 180 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
"The trouble with quotes on the internet is that you can never know if they are genuine."