Andrew Porter
asked on
Batch File using XCOPY Needs To Skip Machines That Are Offline
I've created a batch file that uses XCOPY to iterate through a list of workstations, and copy files from a server to a collection of workstations.
The batch file works great - unless one of the machines is offline, then the .bat just hangs.
Do any of you experts have an idea as to how I can add some conditional handling that will "test" each workstation for reachability, and if the machine is not online, skip it and go to the next. I've seen ideas to test w/ ping, but I don't know how to implement it.
TEXT FOR BATCH FILE
FOR /F "delims=" %i IN (C:\Automation\Workstations.txt) DO (
xcopy "\\servername\sharename\*.pdf" "%i" /Y)
TEXT FOR WORKSTATIONS.TXT
\\workstation1\c$\abc\dir\
\\workstation2\c$\abc\dir\
\\workstation3\c$\abc\dir\
\\workstation4\c$\abc\dir\
\\workstation5\c$\abc\dir\
The batch file works great - unless one of the machines is offline, then the .bat just hangs.
Do any of you experts have an idea as to how I can add some conditional handling that will "test" each workstation for reachability, and if the machine is not online, skip it and go to the next. I've seen ideas to test w/ ping, but I don't know how to implement it.
TEXT FOR BATCH FILE
FOR /F "delims=" %i IN (C:\Automation\Workstations.txt) DO (
xcopy "\\servername\sharename\*.pdf" "%i" /Y)
TEXT FOR WORKSTATIONS.TXT
\\workstation1\c$\abc\dir\
\\workstation2\c$\abc\dir\
\\workstation3\c$\abc\dir\
\\workstation4\c$\abc\dir\
\\workstation5\c$\abc\dir\
ASKER
NVIT - That got me closer. The issue now is that the ping call against workstations.txt pings the entire path for the workstations:
\\workstation1\c$\abc\dir\ instead of just workstation1
Could I reshape my xcopy command to concatenate the destination directory and just keep the workstation names in the workstations.txt file?
Something like:
\\workstation1\c$\abc\dir\ instead of just workstation1
Could I reshape my xcopy command to concatenate the destination directory and just keep the workstation names in the workstations.txt file?
Something like:
Setlocal EnableDelayedExpansion
Set QFP = \c$\abc\dir\
FOR /F "delims=" %ii IN (C:\Automation\Workstations.txt) DO (
ping %%i
if !errorlevel! equ 0 xcopy "\\servername\sharename\*.pdf" "%%i and QFP"
)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm struggling a bit NVIT...
Whenever I use the %% in my batch, I receive this error:
%%i was unexpected at this time
Here is my code:
Whenever I use the %% in my batch, I receive this error:
%%i was unexpected at this time
Here is my code:
@echo off
setlocal enabledelayedexpansion
FOR /f %%i IN ('type "C:\Automation\QF_Workstations.txt"') DO (
for /f "tokens=1 delims=\" %a in ('echo %%i') do set host=%%a
ping !host!
if !errorlevel! equ 0 ECHO xcopy "\\itsfm-file\data\qfw\qfwdb\*.qfdat" "%%i" /Y
)
Line 5: %a in should be %%a in
You do have this code in a .bat or .cmd file, right?
You do have this code in a .bat or .cmd file, right?
ASKER
You need setlocal enabledelayedexpansion
IMO, it's simpler to just run the .bat/cmd file in the Command window. That way you don't need to deal with the %
/C Continues copying even if errors occur.