Link to home
Start Free TrialLog in
Avatar of bfpnaeechange
bfpnaeechange

asked on

batch file to push host file to remote computer

Hi all, I have a need to push out a central host file to remote computers.

del results.txt
FOR /F %%a IN (list.txt) DO XCOPY /y c:\test2.txt \\%%a\c$\windows\system32\drivers\etc >> results.txt
echo %date% %time% %errorlevel%>>results.txt

This is what I have so far.  I would like the results.txt to list the computer name where the file was not copied....right now it says 0 File(s) copied.
I have 400 computers to run this on.....So I need to be able to identify which computers did not get the copy  for whatever reason...
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
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
sirbounty,
I do not believe that %errorlevel% in for will work, as variable substitution is performed when DO is reached, so errorlevel holds the state when FOR is executed.
You will have to use delayed expansion, as in first part of my Snippet.
I agree to your usage of admin$ share.

bfpnaeechange,
the second part is an alternative way to do it, avoiding errorlevel, and introducing some "improvements".


@echo off
setlocal EnableDelayedExpansion
del results.txt 2>nul >nul
FOR /F %%C IN (list.txt) DO (
  XCOPY /y c:\test2.txt \\%%C\admin$\system32\drivers\etc 
  if errorlevel 1 then echo Error with %%C >> results
  echo !date! !time! !errorlevel!>>results.txt
)
 
@REM -- Another way --
@echo off
setlocal EnableDelayedExpansion
FOR /F %%C IN (list.txt) DO (
  set line=!date! !time! %%C
  XCOPY /y c:\test2.txt \\%%C\admin$\system32\drivers\etc >nul 2>&1 || set line=!line! Error 
  echo !line!
) >results.txt

Open in new window