2017-10-09 61 views
0

我曾嘗試過:使用Node JS Child Process API在我的項目文件夾中產生bat文件。但我無法讓它運行。如何運行節點js的windows批處理文件

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

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

錯誤

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

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

我也曾嘗試

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

錯誤

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)

回答

0

更新:我發現這個問題。

我的文件類型是Bat shortcut

上面的代碼只適用於Windows Batch File,但不是shortcut