A: 

If paths don't contain white spaces (and your examples don't) the answer is simple:

String strippedPath = unstrippedPath.replaceFirst(" .*","");
//strippes everything after the first space

However: this will fail for paths like C:\documents and settings\yourname\etc

seanizer
it has to work for any path: paths with white spaces, german umlaute
christian
A: 

Non trivial you could try to improve your parsing by reviewing and incorporating the file system rules; try here for example.

BigMac66
A: 

Indexing backwards would protect you from finding existing file too early in cases like this:

  • C:\My
  • C:\My File

You could add executable suffixes as you test for File.exists, to capture likes of ipconfig.exe from ipconfig.

You probably would NOT want to do this, but you could finally try executing the resulting string to test if it is an executable. This would be rather a dangerous thing to do with arbitrary input.

public class Example
{
    public static void main(String[] args)
    {
        String[] samples = {
            "C:\\windows\\system32\\notepad.exe bla.txt",
            "C:\\WINDOWS\\system32\\ipconfig /all",
            System.getProperty("user.home") + " foo bar"
        };

        for (String s: samples) {
            File f = getFile(s, new String[]{"exe", "cmd"});
            System.out.println(f);

            // you probably don't want to do this
            Process p = null;
            try {
                p = Runtime.getRuntime().exec(f.toString());
            } catch (IOException e) {
                System.err.println("Not executable: " + f);
            } finally {
                if (p != null) {
                    p.destroy();
                }
            }
        }
    }

    public static File getFile(String cmd, String[] suffixes)
    {
        StringBuilder buffer = new StringBuilder(cmd);
        while(true) {
            File f = new File(buffer.toString());
            if (f.exists()) {
                return f;
            } else {
                for (String suffix: suffixes) {
                    f = new File(f.toString() + "." + suffix);
                    if (f.exists()) {
                        return f;
                    }
                }
            }
            int start = buffer.lastIndexOf(" ");
            if (start <= 0) {
                break;
            }
            buffer.delete(start, buffer.length());
        }
        return null;
    }

}
sudocode
+1  A: 

I think the best approach would be to parse the String including correct quotation mark handling and cut off on the first space (or whatever the specification says). Getting the behavior right isn't easy, as BigMac66 already mentioned. But it is cleaner than guessing with FileIO.Exists(str), should be more secure (when implemented correctly) and potentially faster because it doesn't require IO.

A small proof of concept code is pretty easy and short and handles cases like these:

"C:\Users\xod\my file.exe" /run "asdf 123" -> "C:\Users\xod\my file.exe"
C:\Users\xod\test.exe asdf -> "C:\Users\xod\test.exe"

Code:

public String GetExecutable(String cmdline) {

    var executable = new StringBuilder();
    var inquote = false;

    foreach  (var c in cmdline.ToCharArray()) {
        if (c == '\"')
            inquote = !inquote;
        else if (!inquote && c == ' ')
            break;
        else
            executable.Append(c);
    }

    return executable.ToString();
}
xod