How to enter an escape sequence when using ProcessBuilder to open Windows file explorer and highlight the file?

1k views Asked by At

I need to be able to open a Windows Explorer window from my java program and highlight the specified file. I read How to use java code to open Windows file explorer and highlight the specified file? and intend to use the following recommendation from @krock :

Process p = new ProcessBuilder("explorer.exe", "/select,C:\\directory\\selectedFile").start();

But the problem I'm having is that my file path has white spaces, so it's just opening to my home folder. @krock mentioned that you have to escape spaces to get around white spaces, but I must not be doing it right.

Can someone please give an example of exactly how to correctly escape spaces in the following code so that my java program opens Windows Explorer to the correct folder (and still highlights the file)?:

Process newWindowsExplorerWindow = new ProcessBuilder("explorer.exe", "/select,C:\\Users\\Public\\Music\\Sample Music\\mySong.mp3").start();
3

There are 3 answers

0
Evan Bechtol On

You should be able to use \s in place of any white-space character in the file path.

I must admit, I have never had trouble with white-spaces in file paths before using that method, but using \w should work.

See the example below, I have used a similar statement with white-spaces.

Process p = Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe \"" + hprlnkDocumentAvailable.getHref() + "\"");

Using your snippet:

Process newWindowsExplorerWindow = new ProcessBuilder("explorer.exe", "/select,C:\\Users\\Public\\Music\\Sample\\sMusic\\mySong.mp3").start();

I tried this on my computer, but it only opened to my documents, no matter if I used public directory or my documents with a specific file.

0
BigWeirdAlFan On

Thanks, everyone, for providing answers. In the end, the only way I was able to get it to work was to separate the desired action, /select , from the file path, and put quotations around each separately (ending up with 3 arguments, not 2) as follows:

Process newWindowsExplorerWindow = new ProcessBuilder("explorer.exe", "/select,", "C:\\Users\\Public\\Music\\Sample Music\\mySong.mp3").start();
0
clstrfsck On

Unfortunately there seems to be some difficulties with how Java parses/assembles command lines in windows, and how Windows Explorer expects command lines to be presented.

I theory, it should be possible to surround the whole /select,C:\Users\Public\Music\Sample Music\mySong.mp3 string in quotes and have it work. Unfortunately this doesn't work on my machine with Java 8 and Windows.

What does work on my machine is to place a single quote at the front of the /select,... switch like so:

Process newWindowsExplorerWindow = new ProcessBuilder("explorer.exe",
    "\"/select,C:\\Users\\Public\\Music\\Sample Music\\mySong.mp3").start();
//   ^^ Escaped quote character 

No guarantees that this will work with other versions of Windows and/or Java.