my application processes strings with commandlines. I need to get the string to the given executable that is called for example:
"C:\windows\system32\notepad.exe bla.txt" => C:\windows\system32\notepad.exe
"C:\windows\system32\ipconfig /all" => C:\windows\system32\ipconfig
"D:\this is my path\thisismyexe.exe -l this -p are -m my -l parameters" => D:\this is my path\thisismyexe.exe
my current idea is more like a workaround :
String path = cmdLineString;
while (!new File(path).exists() && path.lastIndexOf(" ") != -1) {
path = path.substring(0, path.lastIndexOf(" "));
}
if (new File(path).exists())
//go on
any other useful ideas?
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:
Code: