Link to home
Start Free TrialLog in
Avatar of SgtofMarines
SgtofMarines

asked on

Xcopy Batch file, report back success or failure of hostnames on LAN

Hello Experts,

I am trying to automate a process by using the XCOPY command to multiple computers.  I can get the xcopy to work, but I would like to have the command report back the hostnames in which the copy was successful on, and the same for failures?
It only seems to report back my local computer name which I am running the script from.  
I am running this script locally on a LAN and don’t have access to GP.
Any help is greatly appreciated!

So far this is what I have:

REM *** Copy’s AO folder with file content needed to do administrative functions***

@echo off

FOR /f %%x in (computertest.txt) DO XCOPY  “c:\AO\*"  "\\%%x\c$\AO\"  /y

Echo hostname > \\66BQ1k1\c$\Logs\%computername%

Pause
Avatar of Steven Carnahan
Steven Carnahan
Flag of United States of America image

I have not tested this.
REM *** Copy’s AO folder with file content needed to do administrative functions***

@echo off

FOR /f %%x in (computertest.txt) DO XCOPY  “c:\AO\*"  "\\%%x\c$\AO\"  /y

if exist "\\%%x\c$\AO\" then
Echo hostname > \\66BQ1k1\c$\Logs\%computername% success
else
Echo hostname > \\66BQ1k1\c$\Logs\%computername% failure

Pause

Open in new window

Avatar of SgtofMarines
SgtofMarines

ASKER

Thanks for your quick response, unfortunately it did not work, here is the output which failed:

C:\PStools>REM ::::Copys AO folder w
 functions::::::
0 File(s) copied
0 File(s) copied
0 File(s) copied
'else' is not recognized as an inter
operable program or batch file.
Press any key to continue . . .

It wrote my local computer name to the log folder as well.

I know its just something minor that needs to be adjusted
I made some changes for it to work on my system(s) that I can change back if you need me to but this is the code I came up with and the file attached is the results.txt after I ran it. I only had the one computer in computertest.txt.
REM *** Copy’s AO folder with file content needed to do administrative functions***

@echo off

setlocal EnableExtensions EnableDelayedExpansion

echo > c:\Logs\results.txt

FOR /f %%x in (computertest.txt) DO (

XCOPY "c:\AO\*" "\\%%x\c$\AO\" /y

if exist "\\%%x\c$\AO\" (Echo "%%x success" >> c:\Logs\results.txt) else (echo "%%x failure" >> c:\Logs\results.txt)

)

Pause

Open in new window

results.txt
Hi, this definetely got me closer, the results are outputting now.  The only thing its not doing, is its not copying the AO folder over the the hosts local C: drive, which is weird, cause it cleary is defined in the script.  The Results.txt file reads this:

ECHO is off.
"IRM-BOS-02 failure"
"27MVNJ1 failure"
"3ZTVZ61 failure"

I also physically checked via \\netbios name.  

I really appreciate your help thus far.
There appears to be a permissions issue on the machines administrative share then.

I added a couple of locations to my computertest.txt that I know I had access to and my results were:

ECHO is on.
"LENOVO-0c784a28 failure"                                       No access - it didn't work
"iib-apps\home\sscarnah success"                           Actual share so it created a folder called c$ and placed the folder AO inside that folder - it sort of worked
"iib-apps success"                                                     Have access to administrative share - it worked
This is what I have in my computertest.txt to get the results I recieved.:

LENOVO-0c784a28
iib-apps\home\sscarnah
iib-apps
Give this a try.

~bp
@echo off
REM *** Copy’s AO folder with file content needed to do administrative functions***
 
setlocal EnableDelayedExpansion
if exist "\\66BQ1k1\C$\Logs\%computername%" del "\\66BQ1k1\C$\Logs\%computername%"
 
for /F %%X in (computertest.txt) do (
  xcopy "C:\AO\*" "\\%%X\C$\AO\" /Y && set Status=Success || set Status=Failure
  echo %%X = !Status!>"\\66BQ1k1\C$\Logs\%computername%"
)
 
pause

Open in new window

Hi Pony,

If I script up a basic xcopy command line, it goes through fine, so I dont think it would be a permission issue?  I just think it has to do with some of the script parameters, it has to be something minor which needs an adjustment.

Billprew:  Your script did not work unfortunately.

Thanks guys! I know we are close, I have been trying on my end as well.
==> Billprew:  Your script did not work unfortunately.

Can you provide any additional information to help debug it?

~bp
HI Bill, sorry about not providing the details, after running the script you proposed, I get this following content error:

0 File(s) copied
0 File(s) copied
0 File(s) copied
Press any key to continue . . .

after I hit any key to continue, the command box goes away and nothing happens.

Are these computers all on a domain? The one that fails for me does not have, or rather has a disabled computer account on the domain. The two that passed were on a server (the same server)
Yes, all computers are on a domain, XP Professional, windows firewalls are turned off.  The 3 computers are powered on and I can access them via a run prompt (\\%computername%)....
I just tested by adding a computer account that is NOT disabled on my domain (IBMMJNZ447) and the these are the new results:

ECHO is on.
"LENOVO-0c784a28 failure"
"10.2.4.202 failure"
"iib-apps\home\sscarnah success"
"iib-apps success"
"IBMMJNZ447 success"

The first two are the computer with the disabled account.

Can you REM the @ECHO OFF and look for errors?
REM *** Copy’s AO folder with file content needed to do administrative functions***

rem @echo off

setlocal EnableExtensions EnableDelayedExpansion

echo > c:\Logs\results.txt

FOR /f %%x in (computertest.txt) DO (

XCOPY "c:\AO\*" "\\%%x\c$\AO\" /y

if exist "\\%%x\c$\AO\" (Echo "%%x success" >> \\66BQ1k1c$\Logs\results.txt) else (echo "%%x failure" >> \\66BQ1k1\c$\Logs\results.txt)

)

Pause

Open in new window

Line 13 needs to be changed. I missed a "\"

if exist "\\%%x\c$\AO\" (Echo "%%x success" >> \\66BQ1k1c$\Logs\results.txt) else (echo "%%x failure" >> \\66BQ1k1\c$\Logs\results.txt)

Should be:

if exist "\\%%x\c$\AO\" (Echo "%%x success" >> \\66BQ1k1\c$\Logs\results.txt) else (echo "%%x failure" >> \\66BQ1k1\c$\Logs\results.txt)

HI Pony10us,

I performed that change you said, made some progress as the bactch file ran, but this was the output below.  

Nothing was copied, and the log file says failure for all pc's.  Please see below:


C:\PStools>REM *** CopyÆs AO folder with file content needed to do administrativ
e functions***

C:\PStools>rem @echo off

C:\PStools>setlocal EnableExtensions EnableDelayedExpansion

C:\PStools>echo  1>c:\Logs\results.txt

C:\PStools>FOR /F %x in (computertest.txt) DO (
XCOPY "c:\AO\*" "\\%x\c$\AO\" /y
 if exist "\\%x\c$\AO\" (Echo "%x success"  1>>\\66BQ1k1\c$\Logs\results.txt )
else (echo "%x failure"  1>>\\66BQ1k1\c$\Logs\results.txt )
)

C:\PStools>(
XCOPY "c:\AO\*" "\\IRM-BOS-02\c$\AO\" /y
 if exist "\\IRM-BOS-02\c$\AO\" (Echo "IRM-BOS-02 success"  1>>\\66BQ1k1\c$\Logs
\results.txt )  else (echo "IRM-BOS-02 failure"  1>>\\66BQ1k1\c$\Logs\results.tx
t )
)
Invalid path
0 File(s) copied

C:\PStools>(
XCOPY "c:\AO\*" "\\27MVNJ1\c$\AO\" /y
 if exist "\\27MVNJ1\c$\AO\" (Echo "27MVNJ1 success"  1>>\\66BQ1k1\c$\Logs\resul
ts.txt )  else (echo "27MVNJ1 failure"  1>>\\66BQ1k1\c$\Logs\results.txt )
)
Invalid path
0 File(s) copied

C:\PStools>(
XCOPY "c:\AO\*" "\\D8jb9zb1\c$\AO\" /y
 if exist "\\D8jb9zb1\c$\AO\" (Echo "D8jb9zb1 success"  1>>\\66BQ1k1\c$\Logs\res
ults.txt )  else (echo "D8jb9zb1 failure"  1>>\\66BQ1k1\c$\Logs\results.txt )
)
Invalid path
0 File(s) copied

C:\PStools>pause
Press any key to continue . . .
ASKER CERTIFIED SOLUTION
Avatar of Steven Carnahan
Steven Carnahan
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
HI, it worked!  I seen what the issue was, I had the c:\ao present, but there was nothing inside the folder lol.   Once I copied my files I needed in the folder, it worked!

Thanks for your help!
Great.  Sometimes we just overlook the simple things like my missing the "\"   :)

==> HI, it worked!  I seen what the issue was, I had the c:\ao present, but there was nothing inside
==> the folder lol.   Once I copied my files I needed in the folder, it worked!

Which is likely the same reason your test of my script didn't copy any files...

~bp
And, FWIW, I think my script will detect a broader range of errors than my friend pony10us, which I think just checks if the parent directory will be created.  You might want to improve that slightly to allow better error trapping...

~bp
I agree with bill.  I have been looking at his script and while primarily the same as mine he does have some nice improvements.  Mine only looks for the directory after the xcopy and doesn't really look at anything else.  I believe that his script may even run a bit faster than mine but I haven't tested it.

You might want to give bill's one more try now that you discovered the "missing files" issue and if it works consider splitting the points at the very least.
sure thing, I will try Bills today, if not, tomorrow and report back the status.
Hi BIll, I tried your script and it works, however, it does not report back the status of the host(s) success or fail,  It only reported back my local computer name "66BQ1K1" in my logs folder.
That's rather odd, I reran a test I had created here, which has slightly different folder and share names etc, and it seemed to work okay.  Did you have an "=" after the computer name in the log, or not?

~bp
Hi, I do have a = sign after the computer name in the log file, it shows one "computer name = success".  The other 2 computers arent logged, but the folder was copied to them.
66BQ1K1
Ahhh, that helps.  I see the problem.  Change the following line:

  echo %%X = !Status!>"\\66BQ1k1\C$\Logs\%computername%"

to :

  echo %%X = !Status!>>"\\66BQ1k1\C$\Logs\%computername%"

~bp
That worked!  what does the double >> do? opposed to the single >?  

Thank you very much, I will give u half the points
Now, could you please tell me how to give you half the points? I looked around, but no luck in doing so.
A basic explanation of the difference between > and >> is that you use the > to write to a new file or to overwrite an existing file.  The >> apends to the bottom of an existing file.  

One feature of bill's script is to delete any existing log file at the beginning. If you want to keep the logs for any reason you may wish to add some code to rename/move them. Just a thought.

Don't worry about the points, I think you would need to request the question to be reopened by a moderator, and then reclose it with different point assignment, but it's not a big deal.  Just wanted you to get the most robust solution you could, not worried about the points here.

As far as the difference, the > and >> signs are used whenever you want to take the output of a command that would normally display to the screen by default, and redirect it to a file instead.  A single > says every time that line executes, overwrite the entire file with the output of that command.  On the other hand, two >> says to append to the end of whatever is already in the file.

So if I do this:

del test.txt
echo Line1>test.txt
echo Line2>test.txt
echo Line3>test.txt

and then do

type line.txt

I will only see the last line, since the last echo will overwrite the entire file with that output.  If I do the following though:

del test.txt
echo Line1>>test.txt
echo Line2>>test.txt
echo Line3>>test.txt

and then do

type line.txt

I will see all three lines of output.

Hope this helps,
~bp
bill:  Thank you bill.  I really don't mind sharing though. And that is a much more indepth and clear explanation of the difference between ">" and ">>" than I gave.

SgtofMarines:   By the way, was I correct about bill's code running faster than mine?  I haven't had time to test it yet and was curious.  I wasn't real happy with the speed when I was testing my code.
Yeah, it seemed to run a bit more quicker.  I appreciate both of your help.

I have opened up a new question, if you can help, please feel free.

The question is in regards to running a batch file which reports back the usernames logged in and PC names back to me in a text file.  This would have to run on my LAN or WAN.

reward is 250