tags:

views:

1000

answers:

1

Hi all,

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

+4  A: 

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
Ryan Conrad
Great Regex, added names for all the capturing groups to make it more undestandable when parsing...How does the ftpd which uses this format show years in the modify date?
ullmark
if the year of the modified date is the current year, then it shows only the MMM dd and hh:mm, but if its from a previous year, then it shows the actual year, but no time.
Ryan Conrad
With groups:^(?<fileordir>[d-])(?<attribs>[rwxt-]{3}){3}\s+\d{1,}\s+.*?(?<filesize>\d{1,})\s+(?<date>\w+\s+\d{1,2}\s+(?:\d{4})?)(?<yearortime>\d{1,2}:\d{2})?\s+(?<filename>.+?)\s?$If the year is the same, then it will show time, otherwise it will show year. That is by design. If you need an accurate timestamp, use WebRequestMethods.Ftp.GetDateTimestamp.
adzm