Solved

batch file to push host file to remote computer

Posted on 2009-04-09
2
754 Views
Last Modified: 2012-05-06
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...
0
Comment
Question by:bfpnaeechange
2 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 24108873
del results.txt
FOR /F %%a IN (list.txt) DO (
  XCOPY /y c:\test2.txt \\%%a\admin$\system32\drivers\etc
  if errorlevel==1 then echo Error with %%a >> results
  echo %date% %time% %errorlevel%>>results.txt
  )
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 24121681
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now