VBscript Robocopy语法

时间:2012-07-06 14:12:36

标签: vbscript robocopy

我遇到的问题是我认为VBscript中有关运行robocopy的语法错误。

以下是我现在用来尝试运行robocopy的代码片段:

Dim Command2

sLocalDestinationPath = "C:\Script\files\outzips\"
sFinalDestinationPath = "C:\CopyTestFolder\"


Command2 = "Robocopy.exe " & sLocalDestinationPath & " " & sFinalDestinationPath 

问题是该命令不会产生任何错误,但它也不会将任何文件从本地路径复制到最终路径。从命令行执行时,它运行完全正常。任何帮助都将非常感激,因为这个简单的命令使我无法完成此脚本的其余部分。

我也让它回显出命令,命令与我在命令行中完全匹配。

谢谢,如果您需要解释,请告诉我。

1 个答案:

答案 0 :(得分:1)

您没有说出如何“运行”Robocopy,但我认为它是通过WScript.Shell.Run()

我碰巧没有Robocopy方便,但我确实使用Windows XCopy编写了一个示例。也许您可以调整我的简单XCopy示例,以便使用Robocopy更深入地了解您的问题。

Option Explicit

' XCOPY doesn't Like trailing slashes in folder names
Const sLocalDestinationPath = "C:\Script\files\outzips"
Const sFinalDestinationPath = "C:\CopyTestFolder"

Dim Command2 : Command2 = _
    "XCOPY" _
    & " " & sLocalDestinationPath _
    & " " & sFinalDestinationPath _
    & " /E /I /Y" _
    & ""

Dim oSh : Set oSh = CreateObject("WScript.Shell")

WScript.Echo "Cmd: [" & Command2 & "]"

On Error Resume Next
Dim nRetVal : nRetval = oSh.Run(Command2, 0, True)

If Err Then
    WScript.Echo "An exception occurred:" _
           & vbNewLine & "Number: [" & Hex(Err.Number) & "]" _
           & vbNewLine & "Description: [" & Err.Description & "]" _
           & ""
Else
    If nRetVal Then
         WScript.Echo "Copy error: [" & nRetVal & "]"
    Else
         WScript.Echo "Copy succeeded."
    End If
End If

Set oSh = Nothing

' XCOPY options:
'
' /E    Copies directories and subdirectories, including empty ones.
'       Same as /S /E. May be used to modify /T.
'
' /I    If destination does not exist and copying more than one file,
'       assumes that destination must be a directory.
'
' /Y    Suppresses prompting to confirm you want to overwrite an
'       existing destination file.

' End