Questions

Batch File Help

Tags:
+
0 Votes
Locked

Batch File Help

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.
echo Cannot find %fnm%
echo.
Pause
goto :eof

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

:Sub
echo Testing %1
set state=Pinging.
ping -n 1 %1
if errorlevel 1 set state=Not Pinging.
echo %1 is %state% >> %lnm%
  • +
    0 Votes
    gechurch

    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:

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

    :skip
    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.

  • +
    0 Votes
    gechurch

    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:

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

    :skip
    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.