Reputation: 21
I work in the IT office of a County Government Agency and we are tasked with, among other things, imaging and setting up computers for the employees. One of our application providers has given us an installer for their application that, upon installation, creates randomized folder names within the parent folder. I am looking for a VBScript that will create a shortcut of a .exe from a directory with unknown sub-folder names and place it in the Public Desktop folder. I have also discovered that the vendor has included two instances of the same application in two different sub-folders. I am only interested in using the path from the first .exe located. I found a script online, (unfortunately, I do not remember where I found it. So, I am unable to give credit to the individual who wrote it), that creates a shortcut if the path is known. I have edited the script by adding some variables and including more icon settings for the shortcut. I am extremely new to scripting, so, I am unable to modify this script to find the path to the .exe and then use the path to create the shortcut. The first .exe is located three sub-folders deep and all three folders have randomized names. Any help is greatly appreciated.
' This script creates a shortcut of MyApp and places it in the Public Desktop folder for all users
Option Explicit
Dim objWSH, objFSO, link, desktopPath, AppPath, IconPath, DirPath
DirPath = "C:\Program Files\MyApp Folder\Randomized1\Randomized2\Randomized3"
IconPath = "C:\Program Files\MyApp Folder\Randomized1\Randomized2\Randomized3\ApplicationIcon.ico"
AppPath = "C:\Program Files\MyApp Folder\Randomized1\Randomized2\Randomized3\MyApp.exe"
Set objWSH = WScript.CreateObject("WScript.Shell")
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
desktopPath = "C:\Users\Public\Desktop"
' If file exists define where the shortcut should point to
If objFSO.FileExists(AppPath) Then
set link = objWSH.CreateShortcut(desktopPath & "\MyApp.lnk")
' Define icon settings
link.TargetPath = AppPath
link.IconLocation = IconPath
link.Description = "MyApp"
link.WindowStyle = 2
link.WorkingDirectory = DirPath
link.Save
Else
WScript.Echo "Program file does not exist"
End if
Upvotes: 2
Views: 1098
Reputation: 21
After more research and testing, I decided to use Command Line to find the path. After getting the syntax right, it works flawlessly. I have included my final script below. I hope this can help someone else. I went here, Running command line silently with VbScript and getting output?, to help me with using Command Line in VBScript. I then found this, https://blogs.technet.microsoft.com/heyscriptingguy/2007/11/08/hey-scripting-guy-how-can-i-remove-a-value-from-the-path-environment-variable/, which shows how to use the Replace function so I could remove MyApp.exe from the path. I then added that to my original script and it worked.
' This script creates a shortcut of the MyApp application and places it in the Public Desktop folder for all users.
Option Explicit
Dim objExec, output, objDir, objWSH, objFSO, link, DesktopPath, AppPath, IconPath, DirPath
Set objWSH = WScript.CreateObject("WScript.Shell")
Set objExec = objWSH.Exec("Where /R ""C:\Program Files\MyApp"" ""MyApp.exe"" ")
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
output = objExec.StdOut.ReadLine
DirPath = Replace(output, "\MyApp.exe", "")
IconPath = (DirPath & "\MyAppIcon.ico")
AppPath = (DirPath & "\MyApp.exe")
DesktopPath = "C:\Users\Public\Desktop"
' If file exists define where the shortcut should point to
If objFSO.FileExists(AppPath) Then
set link = objWSH.CreateShortcut(DesktopPath & "\MyApp.lnk")
' Define icon settings
link.TargetPath = AppPath
link.IconLocation = IconPath
link.Description = "MyApp"
link.WindowStyle = 3
link.WorkingDirectory = DirPath
link.Save
Else
WScript.Echo "Program file does not exist"
End If
Upvotes: 0
Reputation:
You should be able to modify this to find the files that you want.
GetFileList returns an 1 dimensional array of FileInformation.
Function getFileList(localRoot, fld, ftpArray) Dim fso, f, baseFolder, subFolder, ftpFile, i Set fso = CreateObject("Scripting.Filesystemobject") If IsNull(fld) Then Set baseFolder = fso.GetFolder(localRoot) Else Set baseFolder = fld End If For Each f In baseFolder.Files If IsNull(ftpArray) Then ReDim ftpArray(0) Else i = UBound(ftpArray) + 1 ReDim Preserve ftpArray(i) End If Set ftpFile = New FileInformation ftpFile.setValues localRoot, fso, f Set ftpArray(i) = ftpFile Next For Each subFolder In baseFolder.SubFolders getFileList localRoot, subFolder, ftpArray Next getFileList = ftpArray End Function Class FileInformation Public FilePath Public FolderPath Public FileExtension Public Sub setValues(localRoot, fso, f) FilePath = f.Path FolderPath = f.ParentFolder.Path FileExtension = fso.GetExtensionName(FilePath) End Sub End Class
This will search all the FileInformation collected.
Const localRootFolder = "C:\Program Files\MyApp Folder" Dim filelist, f filelist = getFileList(localRoot, Null, Null) For Each f In filelist Next
Upvotes: 2