简体   繁体   English

如何从节点bash上运行的javascript代码运行Windows cmd.exe命令?

[英]How to run windows cmd.exe commands from javascript code run on node bash?

Is there a way where I can invoke a windows batch file from inside the javascript code? 有没有一种方法可以从javascript代码内部调用Windows批处理文件? Or any other healthy way to do the below through any node package? 还是通过任何节点包进行以下操作的其他健康方法?

scripts.bat scripts.bat

 ECHO "JAVASCRIPT is AWESOME"
  PAUSE

scripts.js scripts.js中

// Code to read and run the batch file //

On the command prompt: 在命令提示符下:

C:/> node scripts.js

One way to do this is with child_process. 一种方法是使用child_process。 You just have to pass the file you want to execute. 您只需要传递要执行的文件。

const execFile = require('child_process').execFile;
const child = execFile('scripts.bat', [], (error, stdout, stderr) => {
  if (error) {
    throw error;
  }
  console.log(stdout);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM