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();
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.
Using your snippet:
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.