[windows2000] Re: Multiple arguments in a batch

  • From: Jonathan Chan <jchan@xxxxxxxxxxxxx>
  • To: windows2000@xxxxxxxxxxxxx
  • Date: Tue, 10 Sep 2002 02:54:24 -0500

At 11:03 PM 9/9/2002, you wrote:

>Okay, as long as we're talking batches, can anyone tell me how to exit
>out of a for /f loop, or use something other than a for /f?
>Here's a piece of my batch:
>for /f "Skip=3 Tokens=1" %%i in ('ping -n 1 %1') do (set Response=%%1) &
>call :checkResponse %Response%
>REM echo %*>>%LogFile%
>if {%Response%} NEQ {Reply} (call wscript.exe pingalrt.vbs %Domain%
>goto :EOF
>What I want to do is try to ping %1, and if the response is not "Reply
>from..." it should launch a vbs script.  It works fine, but after it
>tests the first line from the response, it moves on to the next one.
>So, if my ping results are:
>Pinging somedomain.com [] with 32 bytes of data:
>Reply from bytes=32 time=99ms TTL=241
>Ping statistics for
>     Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
>Approximate round trip times in milli-seconds:
>     Minimum = 99ms, Maximum = 99ms, Average = 99ms
>It's going to test to see if {Reply} NEQ {Reply}, and then it will move
>to tne next line, {} NEQ {Reply} and next, {Ping} NEQ {Reply} and so on.
>I'd like it to just check the first line and then stop.  I wish there
>where an "exit for" command.  Or who knows, maybe there is.

welcome to the world of goto's and the limitations of batch files. assuming 
i read your email/code correctly (batch files - plague - avoid), here's 
what's happening:

you got an iterative loop starting at line 4 (or 3). unless someone else 
says otherwise, i think you misused the call function, as well as the for 
loop. when call is used, your batch file (caller) is more/less put on 
pause, and a 2nd batch file (callee) is then executed. when the callee is 
done running, control then goes back to the caller where it left off. in 
your case, the batch file is calling itself and starts running at the 
checkResponse label. checkResponse cycles through and exits sending control 
back to the caller at the for loop. hopefully that will explain why your 
batch file is doing more than what you want. the only quick fix i can see 
is to replace call with goto. i wish i didnt say that...that's defeating 
the purpose of a for loop, and goto's can break everything (think of it as 
a control-break). if you're running this against one computer only, try 
using the if conditional, which is easier said then done.

learn vbscript to do simple admin tasks. for most people, it's easier to 
read/understand/debug, and the pro's, imo, outweigh the con's. 
functions/procedures/block programming/flow control/databases/etc.

hope that helps some.

To Unsubscribe, set digest or vacation
mode or view archives use the below link.


Other related posts: