VBscript - "The system cannot find the file specified" - vbscript

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

Related

Running command line: can't find file

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.

Reg not starting vbscript

I've got registry entries inside of a .reg file. The reg file is for making a URI Scheme. It worked when I was passing %1 argument to a .bat file, but I don't like viewing the .bat file, so I want to use the well known VBScript method of hiding the .bat file. How do I pass the arguments to the batch file?
Apps.reg
REGEDIT4
[HKEY_CLASSES_ROOT\Apps]
#="URL:Apps Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\Apps\shell]
[HKEY_CLASSES_ROOT\Apps\shell\open]
[HKEY_CLASSES_ROOT\Apps\shell\open\command]
#="\"M:\\Apps\\Apps.vbs\" \"%1\""
Apps.vbs
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "Apps.bat" & Chr(34), 0
Set WshShell = Nothing
The batch file just uses %1 argument. The problem is getting the argument to the batch file. Currently every time I try to use the URI Scheme with the VBScript, I get "apps://foo is not a valid Win32 application." Any help is appreciated!
This works:
WshShell.Run "Apps.bat /foo", 0
If you want to separate your program path which may include spaces from the command line you could try this:
WshShell.Run """Apps.bat"" /foo", 0

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

How to get program files environment setting from VBScript

In a batch file you can use %PROGRAMFILES% to get the location of the program files directory, how do you do it in a VBScript?
Set wshShell = CreateObject("WScript.Shell")
WScript.Echo wshShell.ExpandEnvironmentStrings("%PROGRAMFILES%")
To get Program Files (x86) use:
Set wshShell = CreateObject("WScript.Shell")
WScript.Echo wshShell.ExpandEnvironmentStrings("%PROGRAMFILES(x86)%")

Resources