tags:

views:

64

answers:

4

I have a command, which might resemble the following:

SYNC C:\Users\Fox Mulder\SyncClient C:\Users\Fox Mulder\SyncServer

This is a command that will be entered via a Console Application. It will be read by Console.ReadLine.

How can I parse the command so that I can get the two seperate directory path's?

I am unable to split on space, because that will result in a split at "Fox Mulder".

How can I parse this easily?

+7  A: 
STW
would be nice...if they can
kenny
You can't guarantee usage patterns... requiring quotes in the command line is fragile.
Dave Swersky
yet this is how most command lines behave. this is common practice
Am
@Dave: Windows itself does. Why can't your own code?
Ken White
not only windows, unix has the same requierment
Am
+3  A: 

Using " would solve it

SYNC "C:\Users\Fox Mulder\SyncClient" "C:\Users\Fox Mulder\SyncServer"

this will be interperted as two seperate strings in the argv

Am
+1  A: 

How about this?

string testString = @"SYNC C:\Users\Fox Mulder\SyncClient C:\Users\Fox Mulder\SyncServer";

int firstIndex = testString.IndexOf(Path.VolumeSeparatorChar);
int secondIndex = testString.LastIndexOf(Path.VolumeSeparatorChar);
string path1, path2;

if (firstIndex != secondIndex  && firstIndex != -1)
{
    path1 = testString.Substring(firstIndex - 1, secondIndex - firstIndex);
    path2 = testString.Substring(secondIndex - 1);

    Console.WriteLine("Path 1 = " + path1);
    Console.WriteLine("Path 2 = " + path2);
} 
Scott P
Fix for relative paths?
Jon Seigel
That worked, good enough for my situation, Thanks
IceHeat
Use the right tools for the right job. If the paths are absolute and we are expecting only two, then it's good enough. It would be easy enough to check that the parsed paths are valid, or that we get 2 and only 2 paths.
Scott P
A: 

Another option is to use a non-valid path character as a path delimeter.

SYNC C:\Users\Fox Mulder\SyncClient?C:\Users\Fox Mulder\SyncServer
Will