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

时间:2021-12-12 02:31:23

am trying to run a test.bat file inside node.js

我在试着做一个测试。bat文件在node . js

here is the code

这是代码

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;

while running this node.js file am getting

在运行这个节点。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 }

4 个解决方案

#1


11  

I have found the solution for it.. and its works fine for me. This opens up a new command window and runs my main node JS in child process. You need not give full path of cmd.exe. I was making that mistake.

我已经找到了解决的办法。它对我来说很好。这将打开一个新的命令窗口,并在子进程中运行我的主节点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);
});

#2


2  

The easiest way I know for execute that is following 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);
});

You could replace "path/to/your/file.bat" by __dirname + "/file.bat" if your file is in the directory of your current script for example.

你可以取代“路径/ / /文件。用" __dirname + "/file打。如果你的文件在当前脚本的目录中。

#3


0  

In windows, I don't prefer spawn as it creates a new cmd.exe and we have to pass the .bat or .cmd file as an argument.'exec' is better option. Example below:

在windows中,我不喜欢衍生,因为它创建了一个新的cmd。exe和我们必须传递。bat或。cmd文件作为参数。“执行”是更好的选择。在下面的例子:

Please note that in windows you need to pass path with double \ e.g. C:\path\batfilename.bat

请注意,在windows中,你需要用双\ C:\path\batfilename.bat传递路径

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

#4


0  

An easier way I know for executing that is the following code :

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

function Process() {
const process = require('child_process');   
var ls = process.spawn('script.bat');
ls.stdout.on('data', function (data) {
  console.log(data);
});
ls.stderr.on('data', function (data) {
  console.log(data);
});
ls.on('close', function (code) {
      if (code == 0)
        console.log('Stop');
      else
        console.log('Start');
});
};

Process();

#1


11  

I have found the solution for it.. and its works fine for me. This opens up a new command window and runs my main node JS in child process. You need not give full path of cmd.exe. I was making that mistake.

我已经找到了解决的办法。它对我来说很好。这将打开一个新的命令窗口,并在子进程中运行我的主节点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);
});

#2


2  

The easiest way I know for execute that is following 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);
});

You could replace "path/to/your/file.bat" by __dirname + "/file.bat" if your file is in the directory of your current script for example.

你可以取代“路径/ / /文件。用" __dirname + "/file打。如果你的文件在当前脚本的目录中。

#3


0  

In windows, I don't prefer spawn as it creates a new cmd.exe and we have to pass the .bat or .cmd file as an argument.'exec' is better option. Example below:

在windows中,我不喜欢衍生,因为它创建了一个新的cmd。exe和我们必须传递。bat或。cmd文件作为参数。“执行”是更好的选择。在下面的例子:

Please note that in windows you need to pass path with double \ e.g. C:\path\batfilename.bat

请注意,在windows中,你需要用双\ C:\path\batfilename.bat传递路径

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

#4


0  

An easier way I know for executing that is the following code :

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

function Process() {
const process = require('child_process');   
var ls = process.spawn('script.bat');
ls.stdout.on('data', function (data) {
  console.log(data);
});
ls.stderr.on('data', function (data) {
  console.log(data);
});
ls.on('close', function (code) {
      if (code == 0)
        console.log('Stop');
      else
        console.log('Start');
});
};

Process();