Saturday 31 October 2020

(Node.js child process) Spawning explorer, with /select option, does not work with spaces in path

I'm writing a Node script that performs a "Reveal file in explorer" function. My code boils down to this:

const {spawn} = require('child_process');

// This works (it opens C:/path/to/file and highlights the file named "WithoutSpaces"
spawn('explorer', ["C:\\path\\to\\file\\WithoutSpaces,", "/select"]);

// However, this does not work. It defaults to opening a file explorer window with my Documents folder.
spawn('explorer', ["C:\\this path\\has spaces,", "/select"]
spawn('explorer', ["C:\\this path\\has spaces\\file.txt,", "/select"]

// Strangely, spaces DO work if I'm not doing the /select option:
spawn('explorer', ["C:\\this path\\has spaces,", "/select"]

Strangely, when I use the /select argument to select a file, it only works when the path does not include any spaces. If the path does have spaces, then it defaults to the Documents folder.

Is there any way around this? Perhaps a way to encode a space with a backslash? Or maybe some other special arguments to the explorer command?



from (Node.js child process) Spawning explorer, with /select option, does not work with spaces in path

No comments:

Post a Comment