2017-06-21 124 views
0

我正嘗試在使用網絡路徑的電子中運行批處理文件。我使用的功能是:電子 - 無法找到網絡路徑

function cpSixHundred() { 
    require('child_process').exec("file:\\\\LSC-SA-NAS1\\Departments\\Information Technology\\Software\\Zebra Label Printer Reset Counters", function(err, stdout, stderr) { 
    if (err) { 
     // Ooops. 
     // console.log(stderr); 
     return console.log(err); 
    } 

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

我得到的錯誤是:

Error: Command failed: \\LSC-SA-NAS1\Departments\Information 
Technology\Software\Zebra Label Printer Reset Counterstest.bat 
'\\LSC-SA-NAS1\Departments\Information' is not recognized as an internal or 
external command, 
operable program or batch file. 

我的理解是不喜歡在網絡路徑的空間。我已經嘗試了許多引號和字符串連接的組合,但仍然沒有運氣。

預先感謝您。

+1

這個問題看起來很相似。也許不是,但值得一看:https://stackoverflow.com/questions/16395612/unable-to-execute-child-process-exec-when-path-has-spaces – Aaron

回答

1

您需要使用單引號字符串並在您的文件路徑中放置雙引號。這

一爲例,從節點JS文件採取:

https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

exec('"/path/to/test file/test.sh" arg1 arg2'); 
//Double quotes are used so that the space in the path is not interpreted as 
//multiple arguments 

編輯:

,如果你能,避免要求模塊中的函數調用,它可以在你的應用程序慢下來,大多數時候這是一個糟糕的做法。

// put all required modules on top of your page 
var childProcess = require('child_process'); 

function cpSixHundred() { 
    childProcess.exec('"file:\\\\LSC-SA-NAS1\\Departments\\Information Technology\\Software\\Zebra Label Printer Reset Counters"', function(err, stdout, stderr) { 
     if (err) { 
     // Ooops. 
     // console.log(stderr); 
     return console.log(err); 
     } 

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