- ' This is a generic script to launch an application.
- ' It reads its configuration from an external INI file.
- ' It will use a special launchCommand if provided, otherwise it runs the path.
- '
- ' USAGE:
- ' wscript.exe runner.vbs <app_key>
- '
- ' EXAMPLE:
- ' wscript.exe runner.vbs edge
- Option Explicit
- ' --- Configuration ---
- Dim strConfigFileName
- strConfigFileName = "apps.ini" ' The name of the INI config file.
- ' -------------------
- Dim objShell, fso, configFile, launchKey, line
- Dim strPath, strLaunchCommand
- Dim scriptDir, bInSection
- ' --- 1. Get the command-line argument ---
- If WScript.Arguments.Count = 0 Then
- WScript.Echo "Error: Missing application key." & vbCrLf & "Usage: wscript.exe runner.vbs <app_key>"
- WScript.Quit
- End If
- launchKey = LCase(WScript.Arguments(0)) ' Use LCase for case-insensitive keys
- ' --- 2. Read the INI Configuration File ---
- Set fso = CreateObject("Scripting.FileSystemObject")
- scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
- strConfigFileName = fso.BuildPath(scriptDir, strConfigFileName)
- If Not fso.FileExists(strConfigFileName) Then
- WScript.Echo "Error: Configuration file not found at:" & vbCrLf & strConfigFileName
- WScript.Quit
- End If
- Set configFile = fso.OpenTextFile(strConfigFileName, 1)
- strPath = ""
- strLaunchCommand = ""
- bInSection = False
- ' --- 3. Parse the INI file to find the app's details ---
- Do While Not configFile.AtEndOfStream
- line = Trim(configFile.ReadLine())
- If line <> "" And Left(line, 1) <> ";" Then
- If Left(line, 1) = "[" And Right(line, 1) = "]" Then
- If LCase(Mid(line, 2, Len(line) - 2)) = launchKey Then
- bInSection = True
- ElseIf bInSection Then
- Exit Do ' Stop reading once we've passed the target section
- End If
- ElseIf bInSection And InStr(line, "=") > 0 Then
- Dim arrPair, key, value
- arrPair = Split(line, "=", 2)
- key = Trim(LCase(arrPair(0)))
- value = Trim(arrPair(1))
- If key = "path" Then
- strPath = value
- ElseIf key = "launchcommand" Then
- strLaunchCommand = value
- End If
- End If
- End If
- Loop
- configFile.Close
- If strPath = "" Then
- WScript.Echo "Error: Application key '" & launchKey & "' not found or is missing 'path' in " & strConfigFileName
- WScript.Quit
- End If
- ' --- 4. The Core Launch Logic ---
- Set objShell = CreateObject("WScript.Shell")
- If strLaunchCommand <> "" Then
- ' If a custom command exists, use it. This is the preferred method.
- objShell.Run strLaunchCommand, 1, False
- Else
- ' Otherwise, fall back to running the executable directly from its path.
- objShell.Run Chr(34) & strPath & Chr(34), 1, False
- End If
- ' --- 5. Clean up ---
- Set objShell = Nothing
- Set fso = Nothing