How to get program files environment setting from VBScript - 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)%")

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

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

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)%") & "..."

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

Launch programs whose path contains spaces

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

Resources