windows - 如何从 Node js在windows上执行.bat脚本

标签 windows node.js batch-file

我有 Node js 文件,它将执行我的 bat 文件。我尝试使用 node js 子进程模块的 exec 但没有成功

让我分享一下我的 node js 脚本:

var startTime = '2014-11-27 17:0:42';
var threadName = '<Thread 0>';
var categoryName ='AlarmCategory';
var alarmLevel = 'Fatal';
var alarmCategory = 'OS';
var alarmMessage = 'corrupt';

var cp = require('child_process');
msg = cp.exec('handler.bat' +" " + startTime ,function (error, stdout, stderr) {
    if (error) {
        console.log(error.stack);
        console.log('Error code: '+error.code);
        console.log('Signal received: '+error.signal);
    }
    console.log('Child Process STDOUT: '+stdout);
    console.log('Child Process STDERR: '+stderr);
});

我的 bat 脚本。此脚本接受输入参数和回显。

 @echo off
set startTime=%1
set thread=%2
set categoryName=%3
set alarmLevel=%4
set alarmCategory=%5
set alarmMessage=%6
Echo #####################
Echo This tool will help you get the users info
Echo #####################
Echo hi %arg1%

现在我只打印一个参数。

我得到的错误:

"C:\Program Files (x86)\JetBrains\WebStorm 8.0.4\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" test\test_cmd.js
Error: Command failed: 'handler.bat' is not recognized as an internal or external command,
operable program or batch file.

最佳答案

我解决了我的问题。我现在正在使用 execFile() 函数,因为我还需要传递参数。请务必注意,当您使用 execFile() 使用执行命令时,请确保您在 exeFile() 的命令中设置了“cwd”选项。由于它查找子进程文件但未找到该文件。直接为 .bat 文件设置完整路径不起作用。

我喜欢这个,

msg = cp.execFile('handler.bat' ,[startTime,threadName] ,{cwd:'/Node Js/baflog/sigma-logger/test'},function (error, stdout, stderr) {
.... ..
...
}

关于windows - 如何从 Node js在windows上执行.bat脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27170129/

相关文章:

windows - 我如何将文件目录(路径)作为参数传递?

node.js - 使用 WebSockets 过滤和排序结果?

file - 根据星期几运行批处理文件的一部分

php - javascript错误地确定了夏令时线(2006年的示例)

windows - 使用批处理脚本将命令输出到文件

java - 为什么 Tomcat8.exe 启动的 JAVA_HOME 与 Windows 环境变量中设置的 JAVA_HOME 不同?

node.js - Bluebird - 如何传播嵌套 promise 中的错误

node.js - 无法在 bitbucket 管道中的 node.js 图像中执行 bash 脚本

windows - .bat 文件中的管道分隔符

batch-file - 如何在 windows/dos 命令上获取文件的最后修改日期