2017-06-21 128 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); 
    }); 
}