Link to home
Start Free TrialLog in
Avatar of Andrew Porter
Andrew PorterFlag for United States of America

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\

Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Can't test this for you now but the /C option for xcopy may be what could do the trick:

  /C           Continues copying even if errors occur.
Avatar of Andrew Porter

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:

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" 
)

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
@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
)

Open in new window

Line 5: %a in should be %%a in

You do have this code in a .bat or .cmd file, right?
I've now realized that I need to use a single % when running from CMD and a %% when running as a batch.

When using single % from command line for testing - I'm getting an error stating: ping request could not find !host!

User generated image
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 %