Launch programs whose path contains spaces - vbscript

I need to launch programs in my local system using VBScript. But I am having trouble with the syntax. This is what I am using right now -
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("iexplore")
Set objShell = Nothing
The above code successfully launches IE. I want to launch other browsers. But, if I use -
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("c:\Program Files\Mozilla Firefox\firefox.exe")
Set objShell = Nothing
it is throwing an error saying that the file or path was not found. I am not sure how the parameter inside the Run() function is taken - should I give the path to an EXE or should I give some DOS commands?!

Try:-
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""c:\Program Files\Mozilla Firefox\firefox.exe""")
Set objShell = Nothing
Note the extra ""s in the string. Since the path to the exe contains spaces it needs to be contained with in quotes. (In this case simply using "firefox.exe" would work).
Also bear in mind that many programs exist in the c:\Program Files (x86) folder on 64 bit versions of Windows.

You van use Exec
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Exec("c:\Program Files\Mozilla Firefox\firefox.exe")
Set objShell = Nothing

It's working with
Set WSHELL = CreateObject("Wscript.Shell")
WSHELL.Exec("Application_Path")
But what should be the parameter in case we want to enter the application name only
e.g in case of Internet Explorer
WSHELL.Run("iexplore")

set shell=CreateObject("Shell.Application")
' shell.ShellExecute "application", "arguments", "path", "verb", window
shell.ShellExecute "slipery.bat",,"C:\Users\anthony\Desktop\dvx", "runas", 1
set shell=nothing

find an .exe file for the application you want to run
example iexplore.exe and firefox.exe and remove .exe
and use it in objShell.Run("firefox")
I hope this helps.

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("firefox")
Set objShell = Nothing
Please try this

Copy the folder, firefox.exe is in and place in the c:\ only. The script is having a hard time climbing your file tree. I found that when I placed the *.exe file in the c:\ it eliminated the error message " file not found."

Related

How do I use spaces with my objShell.CurrentDirectory code?

How can I fix this code so it works correctly? I think it has something to do with the spaces in "Start Menu". The problem comes when I run the file in another drive. For example my P: Drive
Set objShell = WScript.CreateObject("WScript.Shell")
File = "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup"
objShell.CurrentDirectory = File
The environment variables can't be just notated in the path string. You have to use the Shell object method below (ExpandEnv...) to obtain the Environment variable.
This should do what I assume you're trying to do to-date.
Set objShell = WScript.CreateObject("WScript.Shell"): appdata = objShell.ExpandEnvironmentStrings("%appdata%")
File = appdata & "\Microsoft\Windows\Start Menu\Programs\Startup"
objShell.CurrentDirectory = File
Wscript.Echo objShell.CurrentDirectory

VBS - Getting program files folder path?

I am tring to get program files folder in vbs. Tried this without luck;
SET wsc = CreateObject("WScript.Shell")
SET fso = WScript.CreateObject("Scripting.FileSystemObject")
targetpath = wsc.SpecialFolders("ProgramFiles") & "\Google\Chrome\Application\chrome.exe"
It just get the C:\ dir. What is the correct way to do it ?
This TechNet article shows the list of SpecialFolders. Program Files is not among them. This is a limitation of the Windows Script Host. In the same way that the following shows a blank popup
SET wsc = CreateObject("WScript.Shell")
msgbox wsc.SpecialFolders("Awesome")
So instead you have at least 2 options.
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H26&)
Set objFolderItem = objFolder.Self
msgbox objFolderItem.Path
&H26& - Program Files
&H2A& - Program Files (x86)
The other option that I would offer is to use Environment variables like JosefZ suggests.
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles%") & "..."
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles(x86)%") & "..."

How to Create Folders and Subfolders in %AppData%

First of all thank you very much for all the help I’ve found over 3 Vbscript that has save my life during this last six months.
I’m new to Vbscripting and I’m currently working on getting a Vbscript that create folders and copy a file at the same time overwrite that folder and file if they already exist
Folder and subfolders to be created (Avaya) C:\Users\My Username\AppData\Roaming\Avaya\ Avaya\one-X Agent\2.5\
File from (Myfile.txt) C:\Myfile.txt to C:\Users\My Username\AppData\Roaming\Avaya\one-X Agent\2.5\
I get “Path not found” error, but If I leave the path till (Avaya) it creates Avaya Folder but not it’s subfolders C:\Users\My Username\AppData\Roaming\Avaya\
Here’s what I have and thank you in advance
Dim fso, vFolder
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
set objWShell = wScript.createObject("WScript.Shell")
usrName = objWShell.expandEnvironmentStrings("%USERNAME%")
Set fso = CreateObject("Scripting.FileSystemObject")
Set vFolder = fso.CreateFolder("C:\Users\" & usrName & "\AppData\Roaming\Avaya\one-X Agent\2.5\")
CreateFolderDemo = vFolder.Path
The problem is that CreateFolder does not create intermediate folders. The FSO doesn't have a method that does that. It might be easier to use mkdir like this:
Option Explicit
Dim shl
Set shl = CreateObject("WScript.Shell")
Call shl.Run("%COMSPEC% /c mkdir ""%APPDATA%\Avaya\one-X Agent\2.5""",0,true)
Some errors:
You declare fso, but you use objFso
You use %USERNAME% but you should consider %APPDATA% instead
You should use OPTION EXPLICIT to detect typos and undefined variables
You should make your code easier to read by dimming one variable at a time
CreateFolder doesn't create the entire tree, so you need to use FolderExists
For example:
Option Explicit
Dim objWShell
Set objWShell = WScript.CreateObject("WScript.Shell")
Dim appData
appData = objWShell.expandEnvironmentStrings("%APPDATA%")
Dim objFso
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(appData + "\Avaya") Then
objFso.CreateFolder appData + "\Avaya"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent") Then
objFso.CreateFolder appData + "\Avaya\one-X Agent"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent\2.5") Then
objFso.CreateFolder appData + "\Avaya\one-X Agent\2.5"
End If
Lastly, it's not clear why your solution needs to be in VBScript. It appears that your requirements are creating folders and copying files, which means, batch files would probably be a far more simpler.

VB Script to open multiple programs at once

Right im looking for a script that I can click on after I have logged in to open various programs just to save me a bit of time. I have managed to get a script to open one but as a bit of a newbie can someone provide advice.
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run """C:\Program Files (x86)\servicecenter\Run\scguiw32.exe "" ""-
express:dvla.servicecenter.fs.fujitsu.com.12680"""
Set objShell = Nothing
You might be overthinking it a bit to use VBScript or Powershell for this job. A batch file will work.
#echo off
start "c:\Program Files\Folder 1\program.exe"
start "c:\Program Files\Folder 2\program.exe" -switch -argument
exit
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("Path to program")
wscript.sleep (100)
objShell.Run("Path to program")
wscript.sleep (100)
wscript.quit
I do not have scguiw32.exe, so I created simple script which opens file in notepad and in word.
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run "C:\Windows\notepad.exe c:\dump.txt"
objShell.Run """C:\Program Files (x86)\Microsoft Office\Office14\winword.exe"" c:\dump.txt"
Set objShell = Nothing
BTW Instead of vbscript you can use now powershell, and powershell script is much more easy to understand. For example above one will be: Create run.ps1 with content
& 'C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE' c:\dump.txt
notepad.exe C:\dump.txt
Click it right-click and choose Run with Powershell
Here is how to use vbscript to create an array of programs you want to run and then execute each one.
'---Declare Variables
Dim objShell, strprogram1, strProgram2, colprograms, item
'---Create Scripting Shell Object
Set objShell = CreateObject("WScript.Shell")
'---Create Program Variables
strProgram1 = """C:\Program Files (x86)\servicecenter\Run\scguiw32.exe"" ""-express:dvla.servicecenter.fs.fujitsu.com.12680"""
strProgram2 = "C:\Windows\notepad.exe C:\Dump.txt"
'---Add Variables to an Array
colPrograms = Array(strProgram1,strProgram2)
'---Run each program in the array once
For Each item In colprograms
objShell.Run item
Next
WScript.Echo "Done."

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