Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node's exec() not working because of a space in the URI name

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 command

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?

like image 983
dsp_099 Avatar asked Oct 21 '25 03:10

dsp_099


1 Answers

exec treats any spaces in the command parameter string as argument separators, so you need to double-quote the whole path to have it all treated as the path to the command 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.

like image 75
JohnnyHK Avatar answered Oct 23 '25 18:10

JohnnyHK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!