VBSCRIPT 11
Runner.vbs Guest on 8th November 2025 10:17:42 PM
  1. ' This is a generic script to launch an application.
  2. ' It reads its configuration from an external INI file.
  3. ' It will use a special launchCommand if provided, otherwise it runs the path.
  4. '
  5. ' USAGE:
  6. ' wscript.exe runner.vbs <app_key>
  7. '
  8. ' EXAMPLE:
  9. ' wscript.exe runner.vbs edge
  10.  
  11. Option Explicit
  12.  
  13. ' --- Configuration ---
  14. Dim strConfigFileName
  15. strConfigFileName = "apps.ini" ' The name of the INI config file.
  16. ' -------------------
  17.  
  18. Dim objShell, fso, configFile, launchKey, line
  19. Dim strPath, strLaunchCommand
  20. Dim scriptDir, bInSection
  21.  
  22. ' --- 1. Get the command-line argument ---
  23. If WScript.Arguments.Count = 0 Then
  24.     WScript.Echo "Error: Missing application key." & vbCrLf & "Usage: wscript.exe runner.vbs <app_key>"
  25.     WScript.Quit
  26. End If
  27. launchKey = LCase(WScript.Arguments(0)) ' Use LCase for case-insensitive keys
  28.  
  29. ' --- 2. Read the INI Configuration File ---
  30. Set fso = CreateObject("Scripting.FileSystemObject")
  31. scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
  32. strConfigFileName = fso.BuildPath(scriptDir, strConfigFileName)
  33.  
  34. If Not fso.FileExists(strConfigFileName) Then
  35.     WScript.Echo "Error: Configuration file not found at:" & vbCrLf & strConfigFileName
  36.     WScript.Quit
  37. End If
  38.  
  39. Set configFile = fso.OpenTextFile(strConfigFileName, 1)
  40. strPath = ""
  41. strLaunchCommand = ""
  42. bInSection = False
  43.  
  44. ' --- 3. Parse the INI file to find the app's details ---
  45. Do While Not configFile.AtEndOfStream
  46.     line = Trim(configFile.ReadLine())
  47.    
  48.     If line <> "" And Left(line, 1) <> ";" Then
  49.         If Left(line, 1) = "[" And Right(line, 1) = "]" Then
  50.             If LCase(Mid(line, 2, Len(line) - 2)) = launchKey Then
  51.                 bInSection = True
  52.             ElseIf bInSection Then
  53.                 Exit Do ' Stop reading once we've passed the target section
  54.            End If
  55.         ElseIf bInSection And InStr(line, "=") > 0 Then
  56.             Dim arrPair, key, value
  57.             arrPair = Split(line, "=", 2)
  58.             key = Trim(LCase(arrPair(0)))
  59.             value = Trim(arrPair(1))
  60.            
  61.             If key = "path" Then
  62.                 strPath = value
  63.             ElseIf key = "launchcommand" Then
  64.                 strLaunchCommand = value
  65.             End If
  66.         End If
  67.     End If
  68. Loop
  69. configFile.Close
  70.  
  71. If strPath = "" Then
  72.     WScript.Echo "Error: Application key '" & launchKey & "' not found or is missing 'path' in " & strConfigFileName
  73.     WScript.Quit
  74. End If
  75.  
  76. ' --- 4. The Core Launch Logic ---
  77. Set objShell = CreateObject("WScript.Shell")
  78.  
  79. If strLaunchCommand <> "" Then
  80.     ' If a custom command exists, use it. This is the preferred method.
  81.    objShell.Run strLaunchCommand, 1, False
  82. Else
  83.     ' Otherwise, fall back to running the executable directly from its path.
  84.    objShell.Run Chr(34) & strPath & Chr(34), 1, False
  85. End If
  86.  
  87. ' --- 5. Clean up ---
  88. Set objShell = Nothing
  89. Set fso = Nothing

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.