vbs cmd path space - vbscript

I would like to be able to call the following cmd command from within a vbs script:
cmd Client\setupclient.exe /q /targetdir "c:\program files\Microsoft CRM"
I came up with the following vbs script:
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.Run "cmd /c Client\setupclient.exe /q /targetdir c:\program files\Microsoft CRM", 1, true
As far as I am concerned, this would work properly if the targetdir had no spaces, e.g c:\bla. Then the app would be installed in that particular folder.
The obvious question is, how can I define the targetdir with spaces as the path location.
I tried to surround it with ' ' but that didn't work for me. Any suggestions?
Cheers
chris

Paths with spaces are typically enclosed in quote characters ("). In VBScript, to insert a quote character into a string you use double quotes (""). So, your code should look like this:
oShell.Run "cmd /c Client\setupclient.exe /q /targetdir ""c:\program files\Microsoft CRM""", 1, true
Also, I'm not sure if cmd /c is actually needed here, so it might work this way as well:
oShell.Run "Client\setupclient.exe /q /targetdir ""c:\program files\Microsoft CRM""", 1, true

I ended up with
AMPath = "E:\Program Files (x86)\Dropbox\Client\Dropbox.exe"
If FileSyst.Fileexists(AMPath) Then
AMPath = chr(34) & AMPath & chr(34)
OBJ_Shell.run (AMPath)
End If

1、If your OS supports 8.3 filename,you can try short filename:
cd c:\
dir /x
2017/04/17 20:53 <DIR> PROGRA~1 Program Files
2017/04/18 03:40 <DIR> PROGRA~2 Program Files (x86)
Then repalce C:\Program Files\ with PROGRA~1.
2、use two double-quotes within full path.
WScript.CreateObject("WScript.Shell").Run """C:\Program Files\DirName\FileName.exe"" /option1 value1 /option2 vaule2 argv3"

This is not exactly the problem described, in that the called program rather than a parameter contains a space. Googling "whshell.run does not work if filename contains blanks" got me here.
When the called program contains a space in its name, it needs to be triple quoted. (The starting and ending quotes define a string with blanks and the enclosed double quotes are mapped to single quotes within that string.) There are two working examples. The first uses triple quotes. The second effectively removes the blanks from the name. The non-working examples show what not to do (and what I tried first.)
' Drive D:\Program Files\Batch\Monitor.bat with no associated command window
Set WshShell = CreateObject("WScript.Shell")
' These methods work: (Select one)
Return = WshShell.Run("""D:\Program Files\Batch\Monitor.bat""", 0)
' Return = WshShell.Run("D:\.D-DISK\Monitor.bat", 0)
' Note: Here "D:\.D-DISK\Monitor.bat" is a symbolic link to
' "D:\Program Files\Batch\Monitor.bat"
' The following methods fail because of the space in the filename.
' WshShell.Run( chr(34) & D:\Program Files\Batch\Monitor.bat & Chr(34), 0 )
' Return = WshShell.Run("D:\Program Files\Batch\Monitor.bat", 0)
' Return = WshShell.Run(""D:\Program Files\Batch\Monitor.bat"", 0)
Set WshShell = Nothing

Related

VBScript Command window - passing a command syntax

I'm trying to get a command to run in a cmd vindow via VBS. Just like this answer:
How to keep the VBScript command window open during execution
The command I'm trying to issue is this, as written it works in a .cmd file.
"\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec" /x86 /f "\\path\folder\folder with space\Import.dtsx"
I've been unable to get it to work in the syntax from the above answer:
objShell.run "%comspec% /c ""SomeProgram.exe -R & pause""", 1, True
Figure it's a double quote issue, but I can't find it.
(I have to use the whole path to dtexec to force usage of the 16bit version.)
Followup: =======================================================
This works:
oShell.Run "%comspec% /C ""\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec"" /x86 /f c:\temp\Import.dtsx & Pause", 1, True
This does not:
oShell.Run "%comspec% /C ""\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec"" /x86 /f ""c:\temp\temp two\Import.dtsx"" & Pause", 1, True
nor this:
oShell.Run "%comspec% /C ""\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec /x86 /f c:\temp\temp two\Import.dtsx"" & Pause", 1, True
It's that space in the filename argument that is hosing it.
You don't need pause, just tell CMD to keep the window open after the command finishes (/k) instead of closing it (/c):
objShell.Run "%comspec% /k program.exe -R", 1, True
Nested double quotes are only required when you have a path with spaces in it, e.g.:
objShell.Run "%comspec% /k ""C:\some folder\program.exe"" -R", 1, True
Edit: If arguments in your commandline are paths with spaces as well you need quotes around each path and another set of quotes around the entire statement:
objShell.Run "%comspec% /c """"C:\some folder\program.exe"" /p ""foo bar"" & pause""", 1, True

VBscript - "The system cannot find the file specified"

I'm trying to write a short VBScript, which opens "calc.exe" and "wordpad.exe".
Well the problem is that VBScript won't let me open "wordpad.exe". I've tried to run the script as an admin, but this doesn't helped.
My Script looks like this:
Set WshShell = WScript.CreateObject("WScript.Shell")
WSHShell.Run "C:\Program Files\Windows NT\Accessories\wordpad.exe"
WSHShell.Run "C:\Windows\System32\calc.exe"
x=msgbox("Test",4096,Test)
I've also tried to define the path like this:
WSHShell.Run ""C:\Program Files\Windows NT\Accessories\wordpad.exe""
Also not working. I'm getting the message "Expected end of statement"
Is there a solution to open "wordpad.exe" by its path?
Kind regards
The shell uses blanks/spaces as separators. So paths containing blanks/spaces need to be quoted. The way to quote " in VBScript string literals is to double them. So:
WSHShell.Run "C:\Program Files\Windows NT\Accessories\wordpad.exe"
==>
WSHShell.Run """C:\Program Files\Windows NT\Accessories\wordpad.exe"""

How to execute commands with variables as path in vbscript

I can compile my visual basic project Project1.vbp into exe through this command
Set objWshScriptExec=objShell.Exec("cmd.exe /S /C cd ""C:\Program Files (x86)\Microsoft Visual Studio\VB98"" & vb6/make ""C:\Users\Ankit\Desktop\New folder (5)\Project1.vbp"" ""C:\Users\Ankit\Desktop\New folder (5)\Project1.exe""")
But when I take
p="C:\Program Files (x86)\Microsoft Visual Studio\VB98"
m="C:\Users\Ankit\Desktop\New folder (5)\"
And try to execute this command
Set objWshScriptExec=objShell.Exec("cmd.exe /S /C cd "p" & vb6/make "m"Project1.vbp"" "m"Project1.exe""")
It results in error.
kindly provide me some solution to correct this command to compile the project to exe.
You have to concatenate strings using & and also enclose your paths in " quotes. (Inside a string, quotes are escaped as "".)
Set d = objShell.Exec("cmd.exe /S /C cd """ & p & """ & vb6/make """ _
& m & "Project1.vbp"" """ & m & "Project1.exe""")

VBS with Space in File Path

This is what I have and can not get the bat to run, if I move the bat to a folder without spaces in the name it works. My problem is that the actual bat is in a folder with spaces, so I need this to work.
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("%comspec% /K C:\Program Files\ping.bat"), 1, True
You need to quote the file specification:
Run("%comspec% /K ""C:\Program Files\ping.bat""")
I had a similar problem with a directory path in a VBScript that had empty spaces:
E.g.
The following did not work:
objShell.Run("C:\Program Files\NetBeans 8.0.2\bin\netbeans64.exe")
I simply included two extra double quotations on either side of the path and it worked for me:
objShell.Run("""C:\Program Files\NetBeans 8.0.2\bin\netbeans64.exe""")
Try this one
Set objShell = WScript.CreateObject("WScript.Shell")
strCommand = chr(34)&"%comspec% /K C:\Program Files\ping.bat"&chr(34)
objShell.Run strCommand,1,True
I know that this is an old question, but I found a fix that works for me.
It's the double quotes you need.
Try below:
objShell.Run("%comspec% /K " & """C:\Program Files\ping.bat""""), 1, True);

WScript.Shell.run launching robocopy doesn't show destination parameters

i'm trying to get a vbscript (cscript/vbs) to copy files from one network location (my tv show downloads folder) to another network location (my pvr).
i tried to use xcopy but ended up with "parse error" without any context. tried a few things like ensuring files that have spaces are quoted, etc. couldn't work it out.
i tried using scripting.filesystemobject's copyfile method, but this failed with a "bad file name or number" error and googling this one led to many, many dead ends.
i am currently trying to get robocopy to handle the copy since it's generally better than both of the above. but when run from within cscript using WScript.Shell's Run method, the destination, filename and parameters all "fall off".
So here's a snip of the copy part.
if dirmatch > "" then
fileFrom = quote(myloc) ' & fil.name) 'fil.name will not need to be quoted
fileTo = quote(dirmatch) ' puts double-quotes around things if they have spaces in the name
' copyCommand = "%comspec% /k xcopy " & fileFrom & " " & fileTo & " /C /D /Y" ' returns "parse error"
copyCommand = "%comspec% /k robocopy " & fileFrom & " " & fileTo & " " & fil.name & " /R:3 /W:10"' /MOV"
logmsg copyCommand
oShellApp.run copyCommand
else
logmsg "no matching directory found for: " & fil.name
end if
The fileFrom and fileTo are directories that I have previously calculated using filename parsing - just in the format of \\server\path\more path might have spaces\etc\etc. fil.name is the file and it never has spaces - is dot seperated. The spaces in folder names is a requirement for matching reasons in another app I use.
So a given file copy command should look like this:
00:19: %comspec% /k robocopy \\qnap\qdownload\transmission\completed \\pvr\e\tv\MythBusters Mythbusters.S11E03.Hail.Hijinx.HDTV.x264-FQM.mp4 /R:3 /W:10
but robocopy in the new command window (e.g. cmd.exe /k) throws up like this:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Tue Oct 23 21:04:58 2012
Source : \\qnap\qdownload\transmission\completed\
Dest -
Files : *.*
Options : *.* /COPY:DAT /R:1000000 /W:30
------------------------------------------------------------------------------
ERROR : No Destination Directory Specified.
i launched it right, but it can't see past the first parameter. Performing the same command using copy might have been running into the same problem.
How can I get ALL the robocopy parameters to get handed across to the new cmd instance?
p.s. my whole script is here: http://pastebin.com/xgpGJYmU : feel free to optimise it!
Ok, so after stumbling about with this for ages I finally worked out that a character at the start of the destination had an ascii code less than zero. both filesystemobject and asp is funny like that and lets you have stupid scenarios like this. asp unicode implementation: you bastard!
it was there staring me in the face from the very beginning:
00:19: %comspec% /k robocopy \\qnap\qdownload\transmission\completed
\\pvr\e\tv\MythBusters Mythbusters.S11E03.Hail.Hijinx.HDTV.x264-FQM.mp4 /R:3 /W:10
a line break before \pvr\e .. etc I treated it as if it were a wrapping problem in the command prompt, but no a bit of "character by character" exploration of the strings revealed the problem.
In the end I was unable to cut a regex that could convert my wide bytes back to ascii, so had to do a letter-by-letter approach:
Function unicodeToAscii(sText)
Dim x, aAscii, ascval, l
l = len(sText)
If l = 0 Then Exit Function
redim aAscii(l)
For x = 1 To l
ascval = AscW(Mid(sText, x, 1))
If (ascval < 0) Then
ascval = 65536 + ascval ' http://support.microsoft.com/kb/272138
End If
aAscii(x) = chr(ascval)
Next
unicodeToAscii = join(aAscii,"")
End Function
which is a totally sucky hack.
anyay, problem not with robocopy or wsh, but with unicode file systems! solved.
p.s. here's the updated, optimised pastebin of the entire script: http://pastebin.com/s1XtzCGC

Resources