VBS - Getting program files folder path? - vbscript

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

Related

Run all shortcut in the mother path of VBScript [duplicate]

This question already has answers here:
How to run several shortcuts (.lnk files) with a script in Windows?
(3 answers)
Closed 2 months ago.
I want to have a executable VBS that run all windows shortcut in the current folder of my VBS.
My shortcuts are both Excel and PDF.
I've made this code, but i have an error on "WshShell.Run objFile.Path"
Other line look working correctly
Dim FSO
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
GetCurrentFolder = FSO.GetAbsolutePathName(".")
Set objFolder = FSO.GetFolder(GetCurrentFolder)
For Each objFile in objFolder.Files
If LCase(Right(objFile.Path, 4)) = ".lnk" Then
WshShell.Run objFile.Path
End If
Next
I want the mother path adapt to VBS, so i can copy past VBS and put it in other folders
Thanks
The error is due to spaces in one or more of your file paths. Correct this by placing quotes around the path: WshShell.Run """" & objFile.Path & """"
The posted script is running all shortcuts in the current directory, which is not necessarily the script's directory (e.g. if the script is launched via the command line from another folder). To fix that, use: FSO.GetParentFolderName(WScript.ScriptFullName).
Here is the complete, corrected, script:
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
GetCurrentFolder = FSO.GetParentFolderName(WScript.ScriptFullName)
Set objFolder = FSO.GetFolder(GetCurrentFolder)
For Each objFile in objFolder.Files
If LCase(Right(objFile.Path, 4)) = ".lnk" Then
WshShell.Run """" & objFile.Path & """"
WScript.Sleep 1000 'adjust as needed
End If
Next

How to pass flag option in CopyHere [duplicate]

I made a script, which is supposed to copy a bunch of fonts to the Windows font folder. When I run it, I receive the output of the file names I would like copied, but nothing copies. It works when I remove the For loop, and specify file names. Any help appreciated.
Const FONTS = &H14&
sFolder = "c:\FontInstalls\"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(FONTS)
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each oFile In objFSO.GetFolder(sFolder).Files
If UCase(objFSO.GetExtensionName(oFile.Name)) = "TTF" Then
objFolder.CopyHere(oFile)
wscript.echo(oFile)
End if
Next
Fonts need to be installed not copied. Copy the shell's objects rather than underlying files. The shell installs fonts copied into the fonts folder.
Here's the objects you need adding files to a zip.
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"

Delete file within APPDATA folder

I have many PCs that currently have a Personal macro workbook installed. More specifically, they all have a shortcut to a Personal macro workbook on a network drive.
To install that, I went to each PC and ran this VBScript:
Option Explicit
Dim oFSO, strAppData, objShell
Set objShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
strAppData=objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Excel\XLSTART\"
oFSO.CopyFile "H:\Folder\Folder\Folder\PERSONAL 1.xlam - Shortcut.lnk", strAppData, True
Set objShell = Nothing
Set oFSO = Nothing
Now though, I want to remove that shortcut to PERSONAL 1.xlam from the XLSTART folder and copy over a shortcut to a different macro workbook.
This might be really easy, but I'm new to VBS and I haven't found a way to delete a file without having the the exact path. And since the path is going to be unique to each PC, I can't do that here.
You just need to modify one line from the above script. Try the following:
Option Explicit
Dim oFSO, strAppData, objShell
Set objShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
strAppData = objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Excel\XLSTART\"
'here is the modified line
oFSO.DeleteFile strAppData & "PERSONAL 1.xlam - Shortcut.lnk", True
Set objShell = Nothing
Set oFSO = Nothing

CopyHere not working as expected when copying fonts

I made a script, which is supposed to copy a bunch of fonts to the Windows font folder. When I run it, I receive the output of the file names I would like copied, but nothing copies. It works when I remove the For loop, and specify file names. Any help appreciated.
Const FONTS = &H14&
sFolder = "c:\FontInstalls\"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(FONTS)
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each oFile In objFSO.GetFolder(sFolder).Files
If UCase(objFSO.GetExtensionName(oFile.Name)) = "TTF" Then
objFolder.CopyHere(oFile)
wscript.echo(oFile)
End if
Next
Fonts need to be installed not copied. Copy the shell's objects rather than underlying files. The shell installs fonts copied into the fonts folder.
Here's the objects you need adding files to a zip.
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"

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