• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

batch file help pls? copy to mutli PCs inc logging

tommo123

Platinum Member
i've cobbled together a batch file (pretty much a direct copy/paste off the various sites) to ping PCs from a txt file source and write the result to a log file (which are offline/online) etc

i've also got one working which copies a select file to a specific destination going through each hostname in a txt file too.

problem is i have no idea how to get logging working on the copy one. i've tried adding an echo line >> logfile.txt etc which just says successful - even if the PCs are offline.

SET COPIED=successful
FOR /F %%X IN (servers.txt) DO (
copy /Y name.txt "\\%%X\C$\temp\"
IF ERRORLEVEL 1 SET COPIED=not successful
Echo File Copy %COPIED% to %%X >> Logfile.txt
)
@echo Completed.
pause

e.g batch file that pings list of PCs in a file and returns result: (that works fine)

@echo off
setlocal ENABLEEXTENSIONS

set OutputFile=logfile.txt
set ListFile=servers.txt
echo y|del %OutputFile%

for /f "tokens=1,2 delims= " %%a in (%ListFile%) do (
call :Sub "%%a" "%%b"
echo Checking "%%a" "%%b")
notepad "%OutputFile%"
goto :eof

:Sub
ping %1 -n 1 > NUL
if ERRORLEVEL 1 (
set state=boo
set name=%1
set IP=
) else (
set Name=%2
set state=On Line yay
set name=%1
)
echo %Name%,%state% %IP%,%2 >> "%OutputFile%"
goto :eof

can anyone pls modify the 1st for me so it copies the file if the machines online but also writes to the log the hostname and success/lack thereof?

also - recommend a good site that actually guides you on building these yourself (for a noob on this kind of thing)?
 
From what I can tell, in the first code, the only thing being directed to logfile.txt (appended to the end) is the Echo line, unless there's more to your batch file that you just didn't post.
 
there was but i copied the original batch file without any log output and added that in a hurry before leaving work.

i tried different variations and just couldn't get it working in any capacity
 
what is your ultimate goal? and what OS are you working from?
mostly asking because it may be that a tool can easily do this, or that you would benefit from a little powershell-fu or something.

i am trying to stop learning bat files and learn to do things the powershell way [i suck at scripting, but there are so many powershell examples out there i am at least good and copying and editing 😉 ]
 
Last edited:
using win7 and currently it's just copying a file to a specific destination on xp/win7 (folder would exist in all destination PCs)

best i can use is psexec but can't install anything extra though. need to stick to a batch file at best
 
Code:
SET COPIED=successful
FOR /F %%X IN (servers.txt) DO (
copy /Y name.txt "\\%%X\C$\temp\"
IF ERRORLEVEL 1 SET COPIED=not successful
Echo File Copy %COPIED% to %%X >> Logfile.txt
)
@echo Completed.
pause

batch, by default, resolves all variables at the start of the script so anything in a loop can't change. This means that your COPIED variable is already set to successful, and changing it within a loop gets lost. It's one of those strange batch quirks that make it beloved/hated.
You can change this behavior using SETLOCAL ENABLEDELAYEDEXPANSION and enclose your variable (in the loop) in ! instead of %.

However, that seems like overkill in this case. Unless there's a lot more that you plan to add to this script, I'd just do something like this
Code:
FOR /F %%X IN (servers.txt) DO (
  copy /Y name.txt "\\%%X\C$\temp\"
  IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1 echo Copy success to %%X >> Logfile.txt
  IF ERRORLEVEL 1 echo Copy fail to %%X >> Logfile.txt
)
 
Code:
However, that seems like overkill in this case. Unless there's a lot more that you plan to add to this script, I'd just do something like this
[code]
FOR /F %%X IN (servers.txt) DO (
  copy /Y name.txt "\\%%X\C$\temp\"
  IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1 echo Copy success to %%X >> Logfile.txt
  IF ERRORLEVEL 1 echo Copy fail to %%X >> Logfile.txt
)

i love when people know how to code. my brain just is having a hard time learning to interpret most of it as something other than greek. that looks very simple and pretty easy to understand.

id suggest scripting guys to the OP if he wants to get some learning on. lots of powershell, sometimes vb, good explanations about how the script is doing its work, and examples. user comments are often helpful as well.

http://blogs.technet.com/b/heyscrip...-file-to-a-number-of-different-computers.aspx

they are just useful enough to me to copy, edit and perform a task. i just have a real hard time making myself read enough of it to care and learn. /but i should care and learn
 
thansk Merlin

will test on monday.

agree with xSauronx too - would love to know this stuff (less so for batch files etc) but getting variables and the like would make using tasker on my phone so much easier.

thanks for the link too btw - will check on monday too when next in work 😀

thanks all
 
Back
Top