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);
Related
I'm trying to run a few commands from VBScript:
Dim objShell
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "cmd /c C:\Program Files (x86)\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win64_x64\scripts"
objshell.Run "lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property"
WScript.Sleep 500
wshshell.SendKeys "{ENTER}"
But I get this error
biarimport.vbs(4, 1) (null): The system cannot find the file specified.
It seems pretty obvious that either lcm_cli.bat or LCMBiar_Import.property file is not there but it's not the case it's all there and it works fine if I directly run it through CMD.
Running a statement
objShell.run "cmd /c C:\some\folder"
does not change the working directory to that folder. It just spawns a CMD instance, which throws an error that the command C:\some\folder isn't recognized and then closes immediately (/c).
Because the working directory didn't change, the subsequent statment runs in the wrong working directory and thus can't find lcm_cli.bat:
objshell.run "lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property"
Either use the CurrentDirectory property for changing the working directory:
objShell.CurrentDirectory "C:\Program Files (x86)\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win64_x64\scripts"
objshell.Run "lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property"
or run the batch script with its full path:
objShell.Run """C:\Program Files (x86)\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win64_x64\scripts\lcm_cli.bat"" -lcmproperty C:\LCMBiar_Import.property"
Note that any path containing spaces must be put in nested double quotes when used in a Run statement (bot not with the CurrentDirectory property). Also, you don't need cmd /c for starting batch scripts. It's only required if you use CMD built-ins like dir or I/O redirection.
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"""
My script contains the following code.
Set objShell=CreateObject("WScript.Shell")
ObjShell.Run "vb6/make Project1.vbp Project1.exe"
But my script doesn't make my project compile into exe.
I have the visual basic project Project1.vbp in the same folder which contains the script and the path for vb6 is also set in the Environment variable.
However when I tried to compile the project manually into exe it worked fine, by the command given below
vb6/make Project1.vbp Project1.exe
Kindly suggest some way to resolve my script's problem.
Always use full paths in sutch cases, I presume the error is there.
You can catch the error produced in the following way.
Here with a Ruby script I want to run but deliberatly made a mistake in the path.
Set objShell = CreateObject("WScript.Shell")
command = "cmd.exe /S /C ruby ""C:\Users\Gebruiker\ruby\excel\ru.rb"""
Set objWshScriptExec = objShell.Exec(command)
Set objStdOut = objWshScriptExec.StdOut
Set objStdErr = objWshScriptExec.StdErr
While Not objStdOut.AtEndOfStream
WScript.echo objStdOut.ReadLine
Wend
While Not objStdErr.AtEndOfStream
WScript.echo objStdErr.ReadLine
Wend
' ruby: No such file or directory -- C:/Users/Gebruiker/ruby/excel/ru.rb (LoadError)
I made the following changes
Set objWshScriptExec=objShell.Exec("cmd.exe /S /C vb6/make ""Project1.vbp""")
And it worked.
Since I am a beginner in vbscript, I don't know the function of
/S /C
so please let me know.
I'm trying to delete a file like winzip command line using this code
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("C:\Program Files\WinZip\wzuninst.exe wzcline C:\Program Files\WinZip\wzclun.dll")
When I run it on cmd it says Bad name or number. Can someone clarify it for me?
That's not a valid command line.
If you want to run a program you use wshshell.run.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd /k dir c:\windows\*.*"
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