如何使用节点js运行Windows批处理文件

时间:2022-12-20 02:17:19

What I have tried: Using Node JS Child Process API to spawn the bat file in my projectfolder. But I couldnt get it running.

我尝试过:使用Node JS Child Process API在我的项目文件夹中生成bat文件。但是我无法让它运转起来。

 projectfolder/
 ├──src/                       
 |   ├──app                 
 |        ├──home  
 |             ├──home.component.ts 
 |── my.bat (File Type: Bat shortcut)     

home.component.ts

home.component.ts

    let 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);
    });

Error

错误

stderr: 'my.bat' is not recognized as an internal or external command, operable program or batch file.

stderr:'my.bat'不被识别为内部或外部命令,可操作程序或批处理文件。

home.component.ts?71c4:159 child process exited with code 1

home.component.ts?71c4:159子进程退出代码1

I have also tried

我也试过了

const exec = require('child_process').execFile;
    exec('my.bat', function (err, data) {
      console.log(err);
      console.log(data);
    });

with error

有错误

Error: spawn my.bat ENOENT at exports._errnoException (util.js:1050) at Process.ChildProcess._handle.onexit (internal/child_process.js:193) at onErrorNT (internal/child_process.js:367) at _combinedTickCallback (internal/process/next_tick.js:80) at process._tickCallback (internal/process/next_tick.js:104)

错误:在processs.ChildProcess._handle.onexit(internal / child_process.js:193)的onErrorNT(internal / child_process.js:367)的_combinedTickCallback(内部)处的exports._errnoException(util.js:193)处生成my.bat ENOENT /process/next_tick.js:80)at process._tickCallback(internal / process / next_tick.js:104)

1 个解决方案

#1


0  

Update: I found the issue.

更新:我发现了这个问题。

My File Type is a Bat shortcut

我的文件类型是Bat快捷方式

The above code only works for Windows Batch File, but not a shortcut

以上代码仅适用于Windows批处理文件,但不适用于快捷方式

#1


0  

Update: I found the issue.

更新:我发现了这个问题。

My File Type is a Bat shortcut

我的文件类型是Bat快捷方式

The above code only works for Windows Batch File, but not a shortcut

以上代码仅适用于Windows批处理文件,但不适用于快捷方式