Running command line: can't find file - vbscript

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.

Related

How to run VBScript from command line

I am a beginner in VBScript.
I need execute in VBScript this command line:
C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld --defaults-file=C:\Program Files\MySQL\MySQL Server 5.5\my.ini MySQL
I googled it & got to know that we can run VBScript from command line by executing below command and I have tried this:
Set oShell = Wscript.CreateObject("WScript.Shell")
oShell.run "cmd /k ""C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld --defaults-file=C:\Program Files\MySQL\MySQL Server 5.5\my.ini MySQL", 1, True
But the script is stopped because in the path of the file my.ini there are some folder with space in the name, that is Program Files and MySQL Server 5.5
How to do resolve this?
You can save the Filepath in a Variable and run it.
Set oShell = Wscript.CreateObject("WScript.Shell")
Dim path = "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld --defaults-file=C:\Program Files\MySQL\MySQL Server 5.5\my.ini MySQL"
oShell.run "cmd /k " path , 1, True
Good Luck
Jonas
First : To make life easier just use this function to double quotes your strings and debug your command with wscript.echo or a MsgBox before executing it !
SQL_CMD = "cmd /k "& DblQuote("C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld.exe") &_
"--defaults-file="& DblQuote("C:\Program Files\MySQL\MySQL Server 5.5\my.ini") & " MySQL"
wscript.echo SQL_CMD
'--------------------------------------
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'--------------------------------------

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"""

Deleting Winzip Command Line Add On 2.2 vbs

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\*.*"

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);

Resources