admin管理员组

文章数量:1291285

var exec = require('child_process').exec
var cmd = 'C:\\Users\\Johnny Cash\\Desktop\\executeme.exe'

exec(cmd, function(e, stdout, stderr) {
  console.log(e);
  console.log(stdout);
  console.log(stderr);
});

'C:\Users\Johnny' is not recognized as an internal or external mand

This has got to be the newbiest question ever, but how do I escape these paths with spaces on windows? It's cuts off at the space and nothing I do (single or double escapes beforehand) seems to do the trick. Does exec() do some formatting I'm not aware of?

var exec = require('child_process').exec
var cmd = 'C:\\Users\\Johnny Cash\\Desktop\\executeme.exe'

exec(cmd, function(e, stdout, stderr) {
  console.log(e);
  console.log(stdout);
  console.log(stderr);
});

'C:\Users\Johnny' is not recognized as an internal or external mand

This has got to be the newbiest question ever, but how do I escape these paths with spaces on windows? It's cuts off at the space and nothing I do (single or double escapes beforehand) seems to do the trick. Does exec() do some formatting I'm not aware of?

Share Improve this question edited Aug 28, 2015 at 1:45 Bergi 665k161 gold badges1k silver badges1.5k bronze badges asked Aug 28, 2015 at 1:41 dsp_099dsp_099 6,12120 gold badges78 silver badges131 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 10

exec treats any spaces in the mand parameter string as argument separators, so you need to double-quote the whole path to have it all treated as the path to the mand to run:

var cmd = '"C:\\Users\\Johnny Cash\\Desktop\\executeme.exe"'

But it's probably cleaner just to use execFile instead, as its file parameter is always treated as the file path, with a separate args parameter. Then you should be able to omit the double-quote wrapping. execFile is a bit leaner anyway as it doesn't execute a subshell like exec does.

You need to scape the space char from the URI by using ^ (caret) char:

var cmd = 'C:\\Users\\Johnny^ Cash\\Desktop\\executeme.exe'

本文标签: javascriptnode39s exec() not working because of a space in the URI nameStack Overflow