19

I am using appjs * and I want to execute a command to open a folder.

What I have

var path = __dirname + '/folder to open/'; 
// path = C:\Program Files\myapplication/folder to open/
require("child_process").exec("start " + path);

Error

Could not find file C:\Program

What I tried

I already tried to escape the spaces, that didn't work.

var path = __dirname + '/folder to open/'; 
path = path.replace(' ', '\ ');
// path = C:\Program Files\myapplication/folder to open/
require("child_process").exec("start " + path);

When I put the path between quotes, No folder is opened, only another prompt.

var path = "\"" + __dirname + "/folder to open/\"";
path = path.replace(' ', '\ ');
// path = "C:\Program Files\myapplication/folder to open/"
require("child_process").exec("start " + path);

Related bug https://github.com/isaacs/npm/pull/2479

Does anyone has a fix or a workaround?

* link removed

Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
  • Escape spaces? `path = path.replace(' ', '\ ');` Make path a string? `path = '"/path to open/"'; – Andreas Hultgren May 06 '13 at 09:24
  • try putting the path in quotes – Fluffy May 06 '13 at 09:30
  • Escaping spaces doesn't work :( also, path already is a string. – Ron van der Heijden May 06 '13 at 09:31
  • There is an even worst scenario where you can't actually workaround. I'm trying to spawn the execution of "npm" and as npm is installed in "Program Files" (space here) it won't work. I even tried modifying the %PATH% variable by adding "" to the begging and end of every path and that didn't worked neither :( – cSn Sep 12 '14 at 12:41

5 Answers5

20

To open a path than contains spaces, you must replace with a double backslash.

In your code you escaped the space character:

"\ "

What you need to do is escape the backslash character so it makes it into the output string:

"\\ "

Try this:

var path = __dirname + '/folder to open/'; 

// Notice the double-backslashes on this following line
path = path.replace(/ /g, '\\ ');

require("child_process").exec("start " + path);
mkoryak
  • 57,086
  • 61
  • 201
  • 257
f1lt3r
  • 2,176
  • 22
  • 26
5

Well, I fixed it.

Or something like it.

Instead of using

"start " + path

I used

"%SystemRoot%\\explorer.exe \"" + path + "\""

Notice the quotes and the forward slashes.

Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
1

In my case it is fixed by adding double quotes for the path except the first drive name or letter.

import * as path from 'path'; // npm module
const filePath = 'C:/Program Files/my application/file to open/test.txt';

const rootName = path.parse(filePath).root; // "C:/"

const filePathTo = `${rootName}"${filePath.replace(rootName, '')}"`; // C:/"Program Files/my application/file to open/test.txt"  

require("child_process").exec(`start ${filePathTo}`);

The text file will be opened.

0

this works for me

f= file.replace(/ /g,"\\\ ")
Vicky
  • 127
  • 1
  • 4
  • 4
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rosário Pereira Fernandes Feb 16 '18 at 08:55
0

You can also use the old-style 8-character-max/no-space names for each path.

The one I always used was always coding c:\PROGRA~1 instead of c:\Program Files, although this only works on english systems.

If the first 8 characters of any path with more chars are unique, I expect you can do something like newPath = origPath.sub(/\W/g, '').substr(0, 6) + "~1"

Don't have a windows system here, just going by memory.

Eric
  • 2,115
  • 2
  • 20
  • 29