Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从node.js运行windows批处理文件_Node.js_Batch File - Fatal编程技术网

从node.js运行windows批处理文件

从node.js运行windows批处理文件,node.js,batch-file,Node.js,Batch File,我试图在node.js中运行test.bat文件 这是密码 var exec = require('child_process').execFile; case '/start': req.on('data', function (chunk) {}); req.on('end', function () { console.log("INSIDE--------------------------------:"); exec('./ul

我试图在node.js中运行test.bat文件

这是密码

var exec = require('child_process').execFile;

case '/start':
    req.on('data', function (chunk) {});
    req.on('end', function () {
      console.log("INSIDE--------------------------------:");      
       exec('./uli.bat', function (err, data) {
        console.log(err);
        console.log(data);
        res.end(data);
      });
    });
    break;
运行这个node.js文件时,我得到

INSIDE--------------------------------:
{ [Error: Command failed: '.' is not recognized as an internal or ext
nd,
operable program or batch file.
] killed: false, code: 1, signal: null }

我已经找到了解决办法。。它对我来说很好。这将打开一个新的命令窗口,并在子进程中运行我的主节点JS。您不需要给出cmd.exe的完整路径。 我犯了那个错误

var spawn = require('child_process').spawn,
ls    = spawn('cmd.exe', ['/c', 'my.bat']);

ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});

ls.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});

ls.on('exit', function (code) {
console.log('child process exited with code ' + code);
});

我知道的最简单的执行方法是以下代码:

require('child_process').exec("path/to/your/file.bat", function (err, stdout, stderr) {
    if (err) {
        // Ooops.
        // console.log(stderr);
        return console.log(err);
    }

    // Done.
    console.log(stdout);
});

例如,如果您的文件位于当前脚本的目录中,您可以用
\uu dirname+“/file.bat”
替换
“path/to/your/file.bat”

在Windows中,我不喜欢spawn,因为它会创建一个新的cmd.exe,我们必须将.bat或.cmd文件作为参数传递<代码>执行是一个更好的选择。示例如下:

请注意,在Windows中,您需要使用双反斜杠传递路径。例如
C:\\path\\batfilename.bat

const { exec } = require('child_process');
exec("path", (err, stdout, stderr) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(stdout);
});

我知道的一种更简单的执行方法是以下代码:

require('child_process').exec("path/to/your/file.bat", function (err, stdout, stderr) {
    if (err) {
        // Ooops.
        // console.log(stderr);
        return console.log(err);
    }

    // Done.
    console.log(stdout);
});
函数过程(){
const process=require('child_process');
var ls=process.spawn('script.bat');
ls.stdout.on('data',函数(data){
控制台日志(数据);
});
ls.stderr.on('data',函数(data){
控制台日志(数据);
});
ls.on('关闭')功能(代码){
如果(代码==0)
console.log('Stop');
其他的
console.log('Start');
});
};
过程();

如果您发布完整的输出,而不是奇怪裁剪的部分,那就更好了:)。另外,
/
路径看起来非常单一。这是您在.bat文件路径中没有
/
的情况下尝试的完整输出。这在窗口上不起作用。Spawn仅在linux shell env上工作。我可以确认,实际上,它在Windows中可以执行
.bat
文件;使用如下模板字符串:
spawn('cmd.exe',[“/c”,“`.\\BINARIES\\t1.bat`],{env:process.env})我收到错误“未捕获引用错误:未定义要求”。有人知道为什么以及如何修复吗?无论我如何尝试,我都会遇到相同的错误:
错误:命令失败:E:/devProj/instager/start.bat错误:不支持输入重定向,立即退出进程。在emitTwo(events.js:106:13)在ChildProcess.emit(events.js:194:7)在maybeClose(internal/child\u process.js:899:16)在process.ChildProcess._handle.onexit(internal/child\u process.js:226:5)在ChildProcess.ChildProcess.emit.emit(events.js:194:7)在maybeClose(internal/child\u-process:899:16)处,cmd:'E:/devProj/instager/start.bat'