CommandLine pathnames with spaces

I can't figure this one out.  I have not tested this on platforms other than Windows 8.

If a file name with a valid path containing spaces is sent through the commandLine (drag and dropped or double clicked on the file)  juce can't find the file. 

If the same file is opened within the app using a standard file open dialog everything is OK.

Of course it is hard to debug this because it olny happens in release builds. If I set the valid path in the command arguments in properties dialog it works. try the following in a release build with a path name containing spaces.

void initialise (const String& commandLine )
{
....

         if ( !commandLine.isEmpty() )
         {
             File f = File( commandLine );
             if ( !f.exists() )
                 LookAndFeel::playAlertSound();    
}

Please give me a solution.

Did you try to use JUCEApplication::getCommandLineParameterArray() instead?

http://www.juce.com/api/classJUCEApplicationBase.html#a61eedb3499aba883c3e4822a6c2c8973

Debug Tip: make your program pop up a dialog showing the command line it receives on startup. The solution will become entirely obvious.

Windows will automatically "quote" a file path if it has a space in it, to preserve the fact that it must be processed as a single string (since otherwise the space would indicate different tokens). It does not bother if there are no spaces.

Your app is simply making invalid assumptions about the input parameters. The 'bare minimum' solution would be:

File f ( commandLine.unquoted() );

However, it would be wise to properly validate the input before doing that. At the very least, you should first ensure that the string is in fact an absolute path before creating a File from it...

String potentialPath = commandLine.unquoted();
File file;
if (File::isAbsolutePath (potentialPath))
{
   // probably a file
   file = File (potentialPath);
}
else
{
   // probably something else
}

Of course, I wouldn't recommend making any assumption that the input string is in fact just a single token. It's definitely sensible to count them first, and decide what to do with them (e.g. if the user drags multiple files onto the exe, quoted or not, the above code will fail to find any of them).

You can do this yourself, or you could use getCommandLineParameterArray as mentioned above.

I did have an AlertWindow to see the commandLine, but I just missed the quotes because I was so focused on the path name itself. Now that you have mentioned it, the problem is obvious. 

 

Thank you for the solution.