Parsing FtpWebRequest ListDirectoryDetails line

zSynopsis picture zSynopsis · Jun 18, 2009 · Viewed 21k times · Source

I need some help with parsing the response from ListDirectoryDetails in C#.

I only need the following fields.

  • File Name/Directory Name
  • Date Created
  • and the File Size.

Here's what some of the lines look like when I run ListDirectoryDetails:

d--x--x--x    2 ftp      ftp          4096 Mar 07  2002 bin
-rw-r--r--    1 ftp      ftp        659450 Jun 15 05:07 TEST.TXT
-rw-r--r--    1 ftp      ftp      101786380 Sep 08  2008 TEST03-05.TXT
drwxrwxr-x    2 ftp      ftp          4096 May 06 12:24 dropoff

Thanks in advance.

Answer

Ryan Conrad picture Ryan Conrad · Aug 25, 2009

Not sure if you still need this, but this is the solution i came up with:

Regex regex = new Regex ( @"^([d-])([rwxt-]{3}){3}\s+\d{1,}\s+.*?(\d{1,})\s+(\w+\s+\d{1,2}\s+(?:\d{4})?)(\d{1,2}:\d{2})?\s+(.+?)\s?$",
    RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace );

Match Groups:

  1. object type:
    • d : directory
    • - : file
  2. Array[3] of permissions (rwx-)
  3. File Size
  4. Last Modified Date
  5. Last Modified Time
  6. File/Directory Name