Search code examples
vbscriptsubroutinewsh

I can't Wscript.Run a command created by a subroutine


I have an array of folder/subfolder combinations. My subroutine grabs the folder combinations, along with constant paths and a provided date-stamp, and builds the command, returning the variable "rcmd" -

For each location in myArray
    Call RoboCopy
    MsgBox rcmd
Next


'Sub for the robocopy command   
Sub RoboCopy
    infolder = location
    arr2 = Split(location,"\")
    outfolder = arr2(0)
    rcmd = "robocopy " & IN_PATH & "\" & infolder & " " & OUT_PATH & "\" _
        & outfolder & " /XO /MAXAGE:2 /NP /Log+:C:\OKC_v13_to_v15_copy_" & stamp & ".log"
End Sub

As you see, I have "MsgBox" helping right now. The MsgBox always displays the command I need, correctly. However, as soon as I try to change it to -

For each location in myArray
    Call RoboCopy
    Wsh.Run rcmd, 0, true
Next

It doesn't do anything. Do I need to supply the command I'm trying to run a different way to Wsh.Run? By the way, Wsh has already been defined as WScript.Shell.


Solution

  • I not use Robocopy but I have 2 things in my mind. First, variable named Wsh not work for me (not sure why). Second, more important, I think, is that you need to call CMD.exe, i.e.:

    Set WshShell = CreateObject("WScript.Shell")
    For each location in myArray
        Call RoboCopy
        WshShell.Run "cmd /c " & rcmd, 0, True
    Next