Answer for:

Batch File Help

Message 2 of 2

View entire thread
0 Votes

Basically everything below :Label1 is reading the computers.txt file and going through each computer and doing 'something' to them. The 'something' is to run everything below :Sub. In your case it pings the computer and then checks to see if there was an error ('errorlevel' is a variable. It gets set automatically for you by DOS - it gets set to 0 if everything worked [ie. the ping got a reply] and gets set to 1 [or a higher number] if the ping fails).

Anyway, what you want to do is copy the file if errorlevel is 0 after the ping. I'm not the best batch programmer in the world, but changing your :Sub to something like the below should get the job done:

echo Testing %1
set state=Pinging.
ping -n 1 %1 > nul
if errorlevel 1 GOTO skip
xcopy D:\FolderToCopy\*.* \\%1\c$\DestinationFolder\

rem Moving on to next PC...
echo .

That does the ping test. If there's an error it jumps down the the :skip section which does nothing useful at all (and the script then jumps back up to :Label1 where it calls :Sub on the next PC). If errorlevel is 0 however it doesn't skip - it will run the xcopy line which copies the folder from the share to the C: drive of the target PC using the hidden C$ share.

Now while the above might work, I doubt it is the best way to solve your problem. If you are on a domain you are better off having a logon script that copies the files. If you are not on a domain then you should expect to have permission issues when you try to connect to the c$ share. You could look into a tool like PsExec (free download with the sysinternals PsTools pack). That lets you run commands on remote computers, and will let you specify a username and password as part of the command.