Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Working with Robocopy

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,54584
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Working with Robocopy
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Working with Robocopy - 12/19/2007 4:24:02 AM   
  drambo


Posts: 8
Score: 0
Joined: 8/7/2007
Status: offline
We run a nightly Robocopy job that backs up files from a directory on each user workstation to a server. Right now, we have to manually drop the hostnames into a text file, which is cumbersome to manage. I've written a VBScript that extracts the workstation names from Active directory, puts them in a recordset, and loops through a process of mapping a drive to each workstation, running the Robocopy job, and then cleaning up after itself. I've also included code to do some logging and create 0k files using Touch.exe that indicate whether or not a particular workstation has backed up successfully.

The part I'm having trouble with is the actual running of the Robocopy job. Here's the snippet of code code I'm using right now for that part of the job:


      

I've verified that strRobocopy creates a valid string, and when I run that string manually from a command prompt, the job runs without a hitch. But for some reason I can't figure out, the Robocopy job just hangs when it's called from my script. Sometimes it copies all the files successfully and then just doesn't quit, and other times it gets part of the way through the copying and stops. Sometimes it hangs when copying from different workstations; it's not always the same one that trips it up.

One interesting thing I've noticed is that when I'm running the script using the debugger and I check the value of objRobocopy.Status, and it returns nothing when I'm expecting a value of 0 or 1.

When I try executing the Robocopy job using the Run method, it runs just fine, but doesn't seem to return the exit codes I'm looking for so that my logging would work, although there's probably some way of extracting that from somewhere. I just don't know how I'd go about trying to obtain that data.

I've tried formatting this a few different way, using Do Until instead of Do While, trying different amounts of time for the VBScript to sleep, etc.,, but nothing's really solved the issue so far. Anybody out there have any ideas for other stuff I can try?
 
 
Post #: 1
 
 RE: Working with Robocopy - 12/19/2007 4:30:12 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
Well, we can help you with this, but before we do, I am a fan of not changing part of a process that works unless you have to. It sounds like your existing robocopy job works and the only real issue is the creation of the file that holds the host names. Why not just script that? Then you don't have to worry about running robocopy via script at all.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to drambo)
 
 
Post #: 2
 
 RE: Working with Robocopy - 12/19/2007 4:42:39 AM   
  drambo


Posts: 8
Score: 0
Joined: 8/7/2007
Status: offline
You know, I never really considered that. I just wanted to write a single script that did everything, I guess. It should be no trouble at all to write a script that dumps all the hostnames to the text file; in fact most of the code I'll need is already written from my work on this script.

All that said, I'd still like to figure this out if I can. I'm still kind of new to VB Scripting, so any opportunities to learn something are welcome.

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: Working with Robocopy - 12/19/2007 4:50:44 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
Ok, let's work on the robocopy command then.

.Exec will not give you access to the robocopy return code. What it does give you access to however is STDOUT. So you can parse STDOUT to see if the robocopy worked or not.

.Run does provide the return code. Could you post the command as you had it when you were doing .Run?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to drambo)
 
 
Post #: 4
 
 RE: Working with Robocopy - 12/19/2007 5:03:16 AM   
  drambo


Posts: 8
Score: 0
Joined: 8/7/2007
Status: offline
Oh, OK. Well that answers one question. So do you think it's possible that Robocopy is hanging because it doesn't agree with being called by .Exec?

Here's the code I used to invoke the job with .Run:


      

I have a feeling you're about to tell me that trying to get the exit code from checking the value of Err.Number leads me off in the wrong direction...

(in reply to ebgreen)
 
 
Post #: 5
 
 RE: Working with Robocopy - 12/19/2007 5:05:45 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
I'm going to tell you exactly that.


Try this:

nReturn = objShell.Run(strRobocopy, 0, True)
WScript.Echo nReturn

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to drambo)
 
 
Post #: 6
 
 RE: Working with Robocopy - 12/19/2007 5:06:20 AM   
  drambo


Posts: 8
Score: 0
Joined: 8/7/2007
Status: offline
One other thing: could you provide me with an example of parsing STDOUT? I'm a little fuzzy on how to use the STDOUT, STDIN, and STDERR properties, or how to extract data from them.

(in reply to ebgreen)
 
 
Post #: 7
 
 RE: Working with Robocopy - 12/19/2007 5:12:16 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
Here is a very simple example of getting STDOUT. Once you have it in a variable, you can parse it just like any other string.


Dim oWsh:Set oWsh = CreateObject("WScript.Shell")
strCmd = "robocopy c:\Temp\RoboTest\folder1 c:\Temp\RoboTest\Temp2"
Set oRobo = oWsh.Exec(strCmd)
While oRobo.Status = 0
WScript.Sleep 1000
Wend
strOut = oRobo.StdOut.ReadAll()
WScript.Echo "ROBOCOPY STDOUT ="
WScript.Echo strOut

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to drambo)
 
 
Post #: 8
 
 RE: Working with Robocopy - 12/19/2007 5:27:54 AM   
  drambo


Posts: 8
Score: 0
Joined: 8/7/2007
Status: offline
Well, heck. That was exactly what I was trying to get it to do. Just goes to show that sometimes, another pair of eyeballs is all you need.

I had no idea that the line:

nReturn = objShell.Run(strRobocopy, 0, True)

would return the exit code and store it in the nReturn variable. I was under the impression that exit codes were always stored in the Err.Number property.

So next question: why does .Run perform this task flawlessly, but .Exec sputter? Is it something to do with Robocopy, or was it just me choosing the wrong tool for the job?

(in reply to ebgreen)
 
 
Post #: 9
 
 RE: Working with Robocopy - 12/19/2007 5:31:03 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
I'm not positive since .Exec worked for robocopy for me. Try grabbing the STDOUT from the robocopy and see if it gives any indication what is going on.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to drambo)
 
 
Post #: 10
 
 RE: Working with Robocopy - 12/19/2007 5:48:49 AM   
  drambo


Posts: 8
Score: 0
Joined: 8/7/2007
Status: offline
OK, I tried that, and Robocopy hung on the first workstation. So I commented out the While/Wend lines and put the script to sleep for 60 seconds, which is more than enough time needed for the Robocopy job to finish. I think that Robocopy terminated when the script resumed, and I got the Echo "ROBOCOPY STDOUT = ". It looks like it's returning a null, but I'm not sure if that's because Robocopy didn't terminate on its own, or because it's something screwy in the relationship between my script and Robocopy.

(in reply to ebgreen)
 
 
Post #: 11
 
 RE: Working with Robocopy - 12/19/2007 6:00:36 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
Hmmm...wierd. Post the line where you set strRobocopy = something from both the .Run code that you used and the .Exec code.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to drambo)
 
 
Post #: 12
 
 RE: Working with Robocopy - 12/19/2007 6:18:17 AM   
  drambo


Posts: 8
Score: 0
Joined: 8/7/2007
Status: offline
That's part of what's odd about this; they're identical. I'm not using a different string for each. Here it is:

strRobocopy = "robocopy " & strDriveLetter & " " & STRBACKUPPATH & strComputerName & " /e /zb /r:0 /w:0 /purge /v"

As I said before, I've verified that this creates a valid string by checking the variable in the debugger. An example of the finished product is:

robocopy t: E:\ubox\BOSTON16 /e /zb /r:0 /w:0 /purge /v

For each machine, the T: drive is mapped to \\hostname\c$\user box. And for this particular machine, it stops copying halfway through and just sits there. It's got to be something with the interplay between the Robocopy and the .Exec method because .Run works with no problem. I've tried this with a variety of combinations of Robocopy switches as well, but nothing seems to unstick it. I've also tried sharing this directory directly without going through the admin share, and that doesn't make any difference either.

(in reply to ebgreen)
 
 
Post #: 13
 
 RE: Working with Robocopy - 12/19/2007 6:27:10 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
Ok, a couple of things to try.

First, try this:

strRobocopy = "cmd /c robocopy " & strDriveLetter & " " & STRBACKUPPATH & strComputerName & " /e /zb /r:0 /w:0 /purge /v"

Also, try switching up the list so that a different machine gets run first.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to drambo)
 
 
Post #: 14
 
 RE: Working with Robocopy - 12/19/2007 6:35:53 AM   
  mbouchard


Posts: 1924
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
You might also want to have robocopy logging while it runs.  I had a strange issue when trying to copy Visual Studio 2008 from the DVD to a network location and it would crap out on one file, happened on multiple Vista systems.  Popped it into an xp system and it copied with no issues.

here is what I got in the log file, over and over until I killed RC.

quote:


       New File           238.2 m    cab2.cab
0.0%
0.8%
1.6%
2.5%
3.3%
4.1%
5.0%
5.8%
6.7%
7.5%
8.3%
9.2%
10.0%
10.9%
11.7%
12.5%
13.4%
14.2%
15.1%
15.9%
16.7%
17.6%
18.4%
19.3%
20.1%
20.9%
21.8%
22.6%
23.5%
24.3%
25.1%
26.0%
26.8%
2007/12/19 12:11:09 ERROR 1 (0x00000001) Copying File e:\msdn\cab2.cab
Incorrect function.

Waiting 30 seconds... Retrying...


_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to ebgreen)
 
 
Post #: 15
 
 RE: Working with Robocopy - 12/19/2007 6:58:51 AM   
  drambo


Posts: 8
Score: 0
Joined: 8/7/2007
Status: offline
OK, the more I dig into this, the more bizarre it gets.

I ran the script with the cmd \c at the beginning of my strRobocopy string. First, I tried it with the workstations in the same order as before, and I got the same result where it hung halfway through the job. So I selected a completely new handful of workstations to test this on, ran it, and it ran OK through the first workstation, and hung on the second workstation.

Out of curiosity, I decided to look and see if any of those files it was copying were in use, and there was a file in use by my user account (obviously, this is the file that it hung while copying). I looked at the file in the source directory, and it had a last modified timestamp of 8/4/2005 9:41 AM. I looked to see if the file showed up in the destination directory, and there it was, but with a last accessed timestamp of 1/1/1980 7:00 PM. No idea what to make of that.

(in reply to ebgreen)
 
 
Post #: 16
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Working with Robocopy Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts