node.js:如何在外部进程中启动dos批处理

时间:2016-03-18 03:39:21

标签: javascript node.js windows batch-file cmd

目前我正在使用此代码:

var spawn = require('child_process').spawn,
  ls = spawn('cmd.exe', ['/c', 'D:/test_bat.cmd']);

ls.stdout.on('data', function (data) {
  myOutputChannel.append('stdout: ' + data);
});

ls.stderr.on('data', function (data) {
  myOutputChannel.append('stderr: ' + data);
});

ls.on('exit', function (code) {
  myOutputChannel.append('child process exited with code ' + code + '\n');
});

这适用于不使用用户输入的批处理文件。

但是如果批次需要任何用户输入,例如它有暂停等待,用户必须输入return才能继续,子进程永远不会结束。 myOutputChannel是一个只写设备,无法处理用户输入。

这就是我需要从node.js代码启动批处理作为外部dos窗口的原因。有什么方法可以在dos窗口关闭时查看node.js代码并获取批处理的退出代码。有没有人知道如何做到这一点?

0 个答案:

没有答案