Batch File Help

By JakenGuard ·
The script below pings computers what i would like to do is add to the script. If the computer pings i want to copy a folder from a shared drive to the computers C Now i have an idea what i need to add i'm just not 100% sure of what i need to add and where I need to add it.

@echo off
set fnm=c:\Script\Pinger\computers.txt
set lnm=c:\Script\Pinger\Results.txt

if exist %fnm% goto Label1

echo Cannot find %fnm%
goto :eof

echo Maintance Pinging STARTED on %date% at %time% > %lnm%
echo ================================================= >> %lnm%
for /f %%i in (%fnm%) do call :Sub %%i
echo ================================================= >> %lnm%
echo Maintance ENDED on %date% at %time% >> %lnm%
echo ... now exiting
goto :eof

echo Testing %1
set state=Pinging.
ping -n 1 %1
if errorlevel 1 set state=Not Pinging.
echo %1 is %state% >> %lnm%

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Do-able, but there's probably a better way

by gechurch In reply to Batch File Help

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 \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.

Related Discussions

Related Forums