Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
916 views
in Technique[技术] by (71.8m points)

node.js - Run .vbs script with node

I am trying to find out how I can run a .vbs file from a node application.

The script does it's own thing and my node application doesn't need any info back except maybe when the script is finished running.

When I find a way to do this I will then look at passing parameters to the script, but for now I just need to know how I can run the script.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use child_process.spawnSync(command[, args][, options]). See a, b. Demo:

Given:

|..
+---vbs
|       slave.vbs
|
---nodejs
        master.js

slave.vbs:

Option Explicit

Dim a : a = "no arg"
If 0 < WScript.Arguments.Count Then a = WScript.Arguments(0) 
Dim o : o = Array("", WScript.ScriptName, a, Time())
o(0) = "MsgBox"
MsgBox Join(o, "|")
o(0) = "StdOut"
WScript.Stdout.WriteLine Join(o, "|")
o(0) = "StdErr" 
WScript.Stderr.WriteLine Join(o, "|")
WScript.Quit 3

master.js:

'use strict';

const
    spawn = require( 'child_process' ).spawnSync,
    vbs = spawn( 'cscript.exe', [ '../vbs/slave.vbs', 'one' ] );

console.log( `stderr: ${vbs.stderr.toString()}` );
console.log( `stdout: ${vbs.stdout.toString()}` );
console.log( `status: ${vbs.status}` );

output:

node master.js

(MessageBox)

stderr: StdErr|slave.vbs|one|14:09:39

stdout: StdOut|slave.vbs|one|14:09:39

status: 3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...