1.

Solve : FOR... IF variable-PC name is off-line get next name from text file - problem?

Answer»

I need help with the following problem:

I want the batch file to run a query on the Scheduled Tasks of all computers that
A) are listed in the text file "computers.txt" and
B) at the time of running the SCRIPT - they are online.

If a computer is off, the FOR loop would pick the next computer name from the text file.

I use PING to check whether the computer is online and use the error level after the response to decide to go ahead with the scheduled task check or pick the next PC.

Here is the code, that I am using, it works, but I was unable to make the script to go the next computer name in case the computer is offline and it runs ALL commands on the offline computer which of course results in errors anyway and it SLOWS the whole script down unnecessarily in case of 60 computers.

===========================================
set errorlevel=0
for /f "DELIMS==" %%A in (computers.txt) do (
     ping -n 1 -w 1 %%A
     if %errorlevel%==0 (
          schtasks /query /s %%A /fo table /nh>>result.log
          schtasks /delete /s %%A /tn defrag_c /f>>result.log
          )
     )
===========================================

I think, some kind of ELSE statement is missing here I just don't KNOW how to intervene with the FOR statement using ELSE of an IF statement.

In this example I had two computer names in the file "computers":
pc1
pc2

and here is the result of running the batch file in the current state:

=============================================================
D:\ADATA\My Files\Batch\Scheduled Tasks>runsetup.bat

D:\ADATA\My Files\Batch\Scheduled Tasks>set errorlevel=0

D:\ADATA\My Files\Batch\Scheduled Tasks>for /F "delims==" %A in (computers.txt) do (
ping -n 1 -w 1 %A
 if 0 == 0 (
schtasks /query /s %A /fo table /nh 1>>result.log
 schtasks /delete /s %A /tn defrag_c /f 1>>result.log
 )
)

D:\ADATA\My Files\Batch\Scheduled Tasks>(
ping -n 1 -w 1 pc1
 if 0 == 0 (
schtasks /query /s gkkglenfile /fo table /nh 1>>result.log
 schtasks /delete /s gkkglenfile /tn defrag_c /f 1>>result.log
)
)

Pinging PC1 [192.168.0.58] with 32 bytes of data:

Reply from 192.168.0.58: bytes=32 time<1ms TTL=128

Ping statistics for 192.168.0.58:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
ERROR: The specified task name "defrag_c" does not exist in the system.

D:\ADATA\My Files\Batch\Scheduled Tasks>(
ping -n 1 -w 1 pc2
 if 0 == 0 (
schtasks /query /s pc2 /fo table /nh 1>>result.log
schtasks /delete /s pc2 /tn defrag_c /f 1>>result.log
)
)
Ping request could not FIND host pc2. Please check the name and try again.
ERROR: The network path was not found.
ERROR: The network path was not found.
D:\ADATA\My Files\Batch\Scheduled Tasks>
=============================================================

I need help here!
The syntax for the errorlevel check is in error. Errorlevels are not numeric values that can be checked arithmetically. The actual compare is the value you declare and above.

Code: [Select]set errorlevel=0
for /f "delims==" %%A in (computers.txt) do (
     ping -n 1 -w 1 %%A
     if not errorlevel 1 (
          schtasks /query /s %%A /fo table /nh>>result.log
          schtasks /delete /s %%A /tn defrag_c /f>>result.log
          )
     )

This should fix you up; the errorlevel is checked for not 1 or greater (meaning zero) and you should not need an else clause in your code.

Good luck. Sidewinder,

Thank you very much for the elegant solution, it works!

(and also thanks for the info on the nature of errorlevel!)

Geza



Discussion

No Comment Found