Mircea M
Mircea M

Reputation: 520

HTA (VBScript) - Shell.Run using variables

I am trying to create a small program that copies a file to another system. the name of the system is previously selected (is a variable = "testsystem"). I have this Sub and Function:

Sub tst
    Dim copythis
    copythis = "xcopy.exe /Y c:\temp\version.bat " & testsystem & "\temp"
    Set objShell = CreateObject("Wscript.Shell")
    Msgbox AddQuotes(copythis)
    objShell.Run AddQuotes(copythis)
End Sub

Function AddQuotes(strInput)
    AddQuotes = Chr(34) & strInput & Chr(34)
End Function

The messageBox displays exactly the string that I need: the full command. Also, if I execute the command manually it works:

C:\temp>xcopy /Y c:\temp\version.bat \\testsystem3\temp
C:\temp\version.bat
1 File(s) copied

I have been battling with this for 2 days now. I guess I am missing some quotes somewhere but can't figure it out.

Thank you!

Upvotes: 0

Views: 9128

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

You're not missing quotes, instead you have to many of them. Your function puts additional double qoutes around the command before it's executed. This causes the entire command string to be interpreted as a single filename (with spaces in it). You'll get the exact same result when you run the command in cmd like this:

C:\temp>"xcopy /Y c:\temp\version.bat \\testsystem3\temp"
The filename, directory name, or volume label syntax is incorrect.

Simply change this line

objShell.Run AddQuotes(copythis)

into this

objShell.Run copythis

and the problem should disappear.

If you want to add double qoutes, add them like this:

copythis = "xcopy.exe /Y " & AddQuotes("c:\temp\version.bat") & " " _
  & AddQuotes(testsystem & "\temp")

Upvotes: 3

Related Questions