source: main/trunk/binaries/windows/bin/background-launcher.vbs

Last change on this file was 35421, checked in by anupama, 3 years ago

New background-launcher.vbs script was committed to wrong location: needs to be in bin\windows\bin, not in bin\script. Related to commit 35416 which uses this script. Repeating commit message for this script: A VBScript to launch an external process (program) in the background. This can then be called from perl instead of using system() because system() does not play nicely with GLI's SafeProcess usage, as it would end up blocking on SafeProcess' join() calls. See the comment in gs2-extensions\open-office\perllib\plugins\OpenOfficeConverter.pm

File size: 2.2 KB
Line 
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
5Option 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
15Dim WinScriptHost
16Set WinScriptHost = CreateObject("WScript.Shell")
17
18
19Dim objStdErr, args
20Set objStdErr = WScript.StdErr
21args = WScript.Arguments.Count
22If 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
26end If
27
28
29Dim strCommand
30Dim i
31strCommand = Chr(34) & WScript.Arguments.Item(0) & Chr(34)
32If 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
38end 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
43WinScriptHost.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
47Set WinScriptHost = Nothing
Note: See TracBrowser for help on using the repository browser.