1 | ' This script is based on https://serverfault.com/questions/9038/run-a-bat-file-in-a-scheduled-task-without-a-window/9042
|
---|
2 | ' to run a given command in the background.
|
---|
3 | ' Usage: CScript //Nologo background-launcher.vbs <Program-To-Launch-In-Background> [arg1] [arg2] ... [argN]
|
---|
4 | ' Going by docx2html.vbs, I think this script must be launched using CScript to make use of stderr
|
---|
5 | Option Explicit
|
---|
6 |
|
---|
7 | ' Reading on VBScript:
|
---|
8 | ' The Option Explicit statement forces the explicit declaration of all variables using the Dim, Private, Public, or ReDim statements.
|
---|
9 | ' Cmdline args: https://ss64.com/vb/syntax-args.html and https://ss64.com/vb/arguments.html for args.length
|
---|
10 | ' Variables: https://www.tutorialspoint.com/vbscript/vbscript_variables.htm
|
---|
11 | ' Set is used to store object refs in a variable: https://www.promotic.eu/en/pmdoc/ScriptLangs/VBScript/Statmn/Set.htm
|
---|
12 | ' Chr(34) is double quote, Chr(32) is space
|
---|
13 | ' Loops: https://ss64.com/vb/do.html
|
---|
14 |
|
---|
15 | Dim WinScriptHost
|
---|
16 | Set WinScriptHost = CreateObject("WScript.Shell")
|
---|
17 |
|
---|
18 |
|
---|
19 | Dim objStdErr, args
|
---|
20 | Set objStdErr = WScript.StdErr
|
---|
21 | args = WScript.Arguments.Count
|
---|
22 | If args < 1 then
|
---|
23 | 'WScript.Echo Usage: CScript //Nologo background-launcher.vbs <Program-To-Launch-In-Background> [arg1] [arg2] ... [argN]
|
---|
24 | objStdErr.Write ("ERROR. Usage: CScript //Nologo " & WScript.ScriptName & " <Program-To-Launch-In-Background> [arg1] [arg2] ... [argN]" & vbCrLf)
|
---|
25 | WScript.Quit
|
---|
26 | end If
|
---|
27 |
|
---|
28 |
|
---|
29 | Dim strCommand
|
---|
30 | Dim i
|
---|
31 | strCommand = Chr(34) & WScript.Arguments.Item(0) & Chr(34)
|
---|
32 | If args > 1 then
|
---|
33 | i = 1
|
---|
34 | Do
|
---|
35 | strCommand = strCommand & Chr(32) & Chr(34) & WScript.Arguments.Item(i) & Chr(34)
|
---|
36 | i = i + 1
|
---|
37 | Loop Until i = WScript.Arguments.Length
|
---|
38 | end If
|
---|
39 | 'e.g. WinScriptHost.Run Chr(34) & "Notepad.exe" & Chr(34) & Chr(32) & Chr(34) & "C:\Users\Anupama\bla.html" & Chr(34), 1
|
---|
40 |
|
---|
41 | ' Set 2nd arg to 0 to run invisibly, pass additional 1 as 3rd arg if wanting to wait until spawned process returns
|
---|
42 | ' See https://docs.microsoft.com/en-us/previous-versions//d5fk67ky(v=vs.85)?redirectedfrom=MSDN
|
---|
43 | WinScriptHost.Run strCommand, 0
|
---|
44 | ' e.g. To run Notepad in background but with its window visible pass in 1:
|
---|
45 | ' WinScriptHost.Run Chr(34) & "Notepad.exe" & Chr(34), 1
|
---|
46 |
|
---|
47 | Set WinScriptHost = Nothing
|
---|