paulkramer
asked on
VBScript to run a robocopy command
Hi All,
I'm trying to run a robocopy command within a VBScript which isn't producing an error, but isn't completing the required file copy either. I need to use VBScript as a list of folders must be iterated through.
This code works if the source and destination dirnames lack spaces. Any ideas?
I'm trying to run a robocopy command within a VBScript which isn't producing an error, but isn't completing the required file copy either. I need to use VBScript as a list of folders must be iterated through.
On Error Resume Next
Set oShell=WScript.CreateObject("WScript.Shell")
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
WScript.Run("robocopy" & " " & Chr(34) & "C:\source\folder one" & Chr(34) & " " & Chr(34) & "C:\dest\folder one" & Chr(34) & " " & "/e /xo /mir /zb /R:5 /W:5 /LOG+:" & Chr(34) & "logs\folder one.log" & Chr(34))
This code works if the source and destination dirnames lack spaces. Any ideas?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
use oShell.Run "%comspec% /k robocopy " & strSource & " " & strTarget & strParms to keep the Cmd box open if the script is erring
If, like me, you're not a big fan of using a bunch of & Chr(34) & (the ASCII code for a double-quote) then you can just insert a double-quote mark inside a string using a double double-quote ("") when you create the string.
an edit of sirbounty's rewrite using double double-quotes would look like this:
How's that for technological double-speak?
an edit of sirbounty's rewrite using double double-quotes would look like this:
Set oShell=WScript.CreateObject("WScript.Shell")
'(Do you even need FSO?) Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
strSource = """C:\source\folder one"""
strTarget = """C:\dest\folder one"""
strParms = " /e /xo /mir /zb /R:5 /W:5 /LOG+:""C:\logs\folder one.log"""
oShell.Run "robocopy " & strSource & " " & strTarget & strParms
Yes, there's THREE double-quotes in there in some spots and only a single actual "double double-quote" but that's because a string is always started with a double-quote and ends with a double-quote. The "triple double-quotes" are actually a "single double-quote" immediately followed by a "double double-quote" to start the string with a double-quote at the beginning of it and vice-versa to end the string with a double-quote at the end of it.How's that for technological double-speak?
The original script looks like it would have worked, only if the WScript.Run was changed to oShell.Run, but either way, sirbounty's is a bit more managable.
Regards,
Rob.