if else

  • Thread starter Thread starter IT STAFF
  • Start date Start date
I

IT STAFF

For /F %%i In (%filename%) Do Call :Ping4Name %%i

:Ping4Name
For /F "Tokens=2" %%a In ('ping -n 1 %1 ^| find /i "Reply"') Do (

cusrmgr.exe -u %admin% -m \\%1 -P %password% > nul
if errorlevel 0 echo %1 successful
)

=========================================================================
my %filename% contains a text file with pc names.

if errorlevel 0 echo %1 successful ==> echo successful when machine is ping
and password changed. For those machines unable to ping, i wish to do a ELSE
statement that echos unsuccessful, but not able to do so.

cusrmgr.exe returns 0 for success, ERROR for not success.

Pls advise
 
IT said:
For /F %%i In (%filename%) Do Call :Ping4Name %%i

For /F "Tokens=2" %%a In ('ping -n 1 %1 ^| find /i "Reply"') Do (

cusrmgr.exe -u %admin% -m \\%1 -P %password% > nul
if errorlevel 0 echo %1 successful
)

=========================================================================
my %filename% contains a text file with pc names.

if errorlevel 0 echo %1 successful ==> echo successful when machine
is ping and password changed. For those machines unable to ping, i
wish to do a ELSE statement that echos unsuccessful, but not able to
do so.
cusrmgr.exe returns 0 for success, ERROR for not success.

Pls advise

The old ERRORLEVEL problem.

IF ERRORLEVEL X

is true if ERRORLEVEL is X OR IS GREATER THAN X

therefore try the syntax

if errorlevel 1 (echo fail) else (echo success)

FWIW, you can also use NOT with IF ERRORLEVEL

so

if NOT errorlevel 1 (echo success) else (echo fail)

should also work - this may save some coding.
 
I've thought of this - Does not work - if NOT errorlevel 1 (echo success)
else (echo fail)

I put some false machines in the %filename%.

Could it be because that the For loop could not ping those machines and it
does not execute the IF command. ?
 
2 IF STAFF
Im sorry but your batch wrong, as i think

For /F %%i In (%filename%) Do (
ping -n 1 %%i 1>nul && cusrmgr.exe -u %admin% -m \\%1 -P
%password% 1> nul || echo %%i not available
)
 
IT said:
I've thought of this - Does not work - if NOT errorlevel 1 (echo
success) else (echo fail)

I put some false machines in the %filename%.

Could it be because that the For loop could not ping those machines
and it does not execute the IF command. ?

Ah - the infamous "Does not work" response with no indication of what it is
that "does not work" or what happened.

What "does not work" is your original batch.

here's your original logical line:

For /F "Tokens=2" %%a In ('ping -n 1 %1 ^| find /i "Reply"') Do (
cusrmgr.exe -u %admin% -m \\%1 -P %password% > nul
if errorlevel 0 echo %1 successful
)

If FIND can't find "reply" (regardless of case) then FOR finds no input at
all and hence it does not execute the DO part, since there is no data to DO
with Hence, if Rely does not occur, cusrmgr.exe does not run.

Since you have not explained cusrmgr or what you actually want to do, we are
reduced to trying to figure it out. You don't use %%a, so the FOR seems
redundant.

Can you replace the "Ping4Name" batch with

cusrmgr.exe -u %admin% -m \\%1 -P %password% > nul
if errorlevel 1 (echo %1 fail) else (echo %1 success)

Or do you really need the PING? Why the "tokens=2" if you aren't using %%a?

Try

ping -n 1 %1 | find /i "Reply" >nul
if errorlevel 1 echo %1 PING FAILED&goto :eof
cusrmgr.exe -u %admin% -m \\%1 -P %password% > nul
if errorlevel 1 (echo %1 fail) else (echo %1 success)
 
For /F "Tokens=2" %%a In ('ping -n 1 %1 ^| find /i "Reply"') Do (

This is not related to your issue but be aware that using "Reply" with ping
is not reliable because ping can return "No Reply from..."

Using "TTL=" is recommended for detecting a valid ping response, if your
version of ping doesn't return errorlevels.
 
foxidrive said:
This is not related to your issue but be aware that using "Reply" with
ping
is not reliable because ping can return "No Reply from..."

Using "TTL=" is recommended for detecting a valid ping response, if your
version of ping doesn't return errorlevels.

to say nothing whatever about 'reply' forming part of %1
 
IT said:
For /F %%i In (%filename%) Do Call :Ping4Name %%i


cusrmgr.exe -u %admin% -m \\%1 -P %password% > nul
if errorlevel 0 echo %1 successful
)

=========================================================================
my %filename% contains a text file with pc names.

if errorlevel 0 echo %1 successful ==> echo successful when machine
is ping and password changed. For those machines unable to ping, i
wish to do a ELSE statement that echos unsuccessful, but not able to
do so.

cusrmgr.exe returns 0 for success, ERROR for not success.


For /F %%i In (%filename%) Do Call :Ping4Name %%i
GOTO :EOF

:Ping4Name
ping -n 1 %1 ^| find /i "TTL=" >nul
if errorlevel 1 (
echo %1 ping unsuccessful
) else (
cusrmgr.exe -u %admin% -m \\%1 -P %password% > nul
if errorlevel 1 (
echo %1 cusrmgr.exe unsuccessful
) else )
echo %1 cusrmgr.exe successful
)
)
 
Back
Top