Using conditional operators in batch files

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

Hello,

tried to use || in a batch file (as a success test to see whether a pc
was on) as outlined below:

note that the pc name (%1) was a parameter passed on the command line

/code/

ping -n 1 %1 || goto ipsnf

nbtstat -a %1

goto end

:ipsnf

echo %1 failed

:end

/endcode/

I've tried variations on a theme with the command including escaping
the || as in the following:

^|^| and ^||

nothing seems to work. Any comments or suggestions?

Thanks in advance

Steve
 
Steve said:
Hello,

tried to use || in a batch file (as a success test to see whether a pc
was on) as outlined below:

note that the pc name (%1) was a parameter passed on the command line

/code/

ping -n 1 %1 || goto ipsnf

nbtstat -a %1

goto end

:ipsnf

echo %1 failed

:end

/endcode/

I've tried variations on a theme with the command including escaping
the || as in the following:

^|^| and ^||

nothing seems to work. Any comments or suggestions?

Thanks in advance

Steve

The conditional operator || picks up an error condition from
the command you run. Pinging a non-existing target does NOT
consistitute an error condition - it is simply unsuccessful. The
usual way to deal with this case requires you to pipe the
output from ping.exe into find.exe and to look for a specific
string, e.g. like so:

ping -n 1 %1 | find /i "bytes=" || echo %1 is not on-line

Your whole code, after deleting the unnecessary bits,
would then look like so:

ping -n 1 %1 | find /i "bytes=" || (echo %1 failed & goto :eof)
nbtstat -a %1

Note that ":eof" is a predefined label in WinXP batch files.
 
Pegasus said:
The conditional operator || picks up an error condition from
the command you run. Pinging a non-existing target does NOT
consistitute an error condition - it is simply unsuccessful. The
usual way to deal with this case requires you to pipe the
output from ping.exe into find.exe and to look for a specific
string, e.g. like so:

ping -n 1 %1 | find /i "bytes=" || echo %1 is not on-line

Your whole code, after deleting the unnecessary bits,
would then look like so:

ping -n 1 %1 | find /i "bytes=" || (echo %1 failed & goto :eof)
nbtstat -a %1

Note that ":eof" is a predefined label in WinXP batch files.

thank you Pegasus. Good explanation, clearly stated. And thanks for
the :eof tip

Steve
 
Back
Top