Sample code for parsing FtpwebRequest response for ListDirectoryDetails



This posting is valid for .Net frameworks 2.0 (Currently released as Whidbey Beta1)


ResponseStream of FtpWebResponse provides the raw data bytes to the user, some of you had asked that it would be more useful to provide methods which return list of directory and files on ListDirectory request to the server. Current .Net frameworks doesn’t support this, so here is some sample code I had written for parsing, in general I found it works very well against most of the server. But just to make sure it is not extensively tested, so treat only as sample, its not guaranteed to work against every server.


using System;
using System.IO;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;


   public struct FileStruct
   {
        public string Flags;
        public string Owner;
        public string Group;
        public bool IsDirectory;
        public DateTime CreateTime;
        public string Name;
    }
   public enum FileListStyle{
        UnixStyle,
        WindowsStyle,
        Unknown  
    }


public class ParseListDirectory
{
       public static void Main(string[] args)
       {
              if(args.Length < 1)
              {
                  Console.WriteLine(“\n Usage FTPListDirParser <uriString>”);
                  return;
              }
              try
              {
                     FtpWebRequest ftpclientRequest = WebRequest.Create(args[0]) as FtpWebRequest;
                     ftpclientRequest.Method = FtpMethods.ListDirectoryDetails;
                     ftpclientRequest.Proxy = null;  
                     FtpWebResponse response = ftpclientRequest.GetResponse() as FtpWebResponse;
                     StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
                     string Datastring = sr.ReadToEnd(); 
                     response.Close();
   
                     FileStruct[]  list = (new ParseListDirectory()).GetList(Datastring);
                     Console.WriteLine (“————After Parsing———–“);
                     foreach(FileStruct thisstruct in list)
                     { 
                            if(thisstruct.IsDirectory)
                                Console.WriteLine(“<DIR> “+thisstruct.Name+”,”+thisstruct.Owner+”,”+thisstruct.Flags+”,”+thisstruct.CreateTime);
                            else
                                  Console.WriteLine(thisstruct.Name+”,”+thisstruct.Owner+”,”+thisstruct.Flags+”,”+thisstruct.CreateTime);
                     }  
           }
           catch(Exception e)
           {
                  Console.WriteLine(e);
           }
    }
 
 private FileStruct[] GetList(string datastring)
 {
  List<FileStruct> myListArray = new List<FileStruct>(); 
  string[] dataRecords = datastring.Split(‘\n’);
  FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords);
  foreach (string s in dataRecords)
  {    
   if (_directoryListStyle != FileListStyle.Unknown && s != “”)
   {
    FileStruct f = new FileStruct();
    f.Name = “..”;
    switch (_directoryListStyle)
    {
     case FileListStyle.UnixStyle:
      f = ParseFileStructFromUnixStyleRecord(s);
      break;
     case FileListStyle.WindowsStyle:
      f = ParseFileStructFromWindowsStyleRecord(s);
      break;
    }
    if (!(f.Name == “.” || f.Name == “..”))
    {
     myListArray.Add(f);     
    }    
   }
  }
  return myListArray.ToArray(); ;
 }


 private FileStruct ParseFileStructFromWindowsStyleRecord(string Record)
 {
  ///Assuming the record style as
  /// 02-03-04  07:46PM       <DIR>          Append
  FileStruct f = new FileStruct();
  string processstr = Record.Trim();
  string dateStr = processstr.Substring(0,8);      
  processstr = (processstr.Substring(8, processstr.Length – 8)).Trim();
  string timeStr = processstr.Substring(0, 7);
  processstr = (processstr.Substring(7, processstr.Length – 7)).Trim();
  f.CreateTime = DateTime.Parse(dateStr + ” ” + timeStr);
  if (processstr.Substring(0,5) == “<DIR>”)
  {
   f.IsDirectory = true;    
   processstr = (processstr.Substring(5, processstr.Length – 5)).Trim();
  }
  else
  {
   string[] strs = processstr.Split(new char[] { ‘ ‘ }, true);
   processstr = strs[1].Trim();
   f.IsDirectory = false;
  }   
  f.Name = processstr;  //Rest is name   
  return f;
 }



 public FileListStyle GuessFileListStyle(string[] recordList)
 {
  foreach (string s in recordList)
  {
   if(s.Length > 10
    && Regex.IsMatch(s.Substring(0,10),”(-|d)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)”))
   {
    return FileListStyle.UnixStyle;
   }      
   else if (s.Length > 8
    && Regex.IsMatch(s.Substring(0, 8),  “[0-9][0-9]-[0-9][0-9]-[0-9][0-9]”))
   {
    return FileListStyle.WindowsStyle;
   }   
  }
  return FileListStyle.Unknown;
 }


 private FileStruct ParseFileStructFromUnixStyleRecord(string Record)
 {
  ///Assuming record style as
  /// dr-xr-xr-x   1 owner    group               0 Nov 25  2002 bussys
  FileStruct f= new FileStruct();   
  string processstr = Record.Trim();        
  f.Flags = processstr.Substring(0,9);
  f.IsDirectory = (f.Flags[0] == ‘d’);  
  processstr =  (processstr.Substring(11)).Trim();
  _cutSubstringFromStringWithTrim(ref processstr,’ ‘,0);   //skip one part
  f.Owner = _cutSubstringFromStringWithTrim(ref processstr,’ ‘,0);
  f.Group = _cutSubstringFromStringWithTrim(ref processstr,’ ‘,0);
  _cutSubstringFromStringWithTrim(ref processstr,’ ‘,0);   //skip one part
  f.CreateTime = DateTime.Parse(_cutSubstringFromStringWithTrim(ref processstr,’ ‘,8));    
  f.Name =  processstr;   //Rest of the part is name
  return f;
 }


     private string _cutSubstringFromStringWithTrim(ref string s, char c, int startIndex)
     {
            int pos1 = s.IndexOf(c, startIndex);
           string retString = s.Substring(0,pos1);
           s = (s.Substring(pos1)).Trim();
           return retString;
       }
}
This posting is provided “AS IS” with no warranties, and confers no rights

Comments (24)

  1. Sim says:

    Getting the following error while doing this:

       using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())

    The Remote Server returned an error:(550) File unavailable(e.g., file not found, no access)

  2. Steffen Xavier xsteffen@ict7.com says:

    Replace:

    string[] strs = processstr.Split(new char[] { ‘ ‘ }, true);

      processstr = strs[1];

    By:

    processstr = processstr.Remove(0,processstr.IndexOf(‘ ‘) + 1);

    Otherwise you truncate file name with white spaces.

    f.CreateTime = DateTime.Parse(dateStr + ” ” + timeStr); Can throw a InvalidFormatException. Replace by

    f.CreateTime = DateTime.Parse(dateStr + ” ” + timeStr, CultureInfo.GetCultureInfo(“en-US”));

    Thanks for you code. He help me.

  3. Atam says:

    Thanks, saved me some time reverse engineering the crap out of this streamreader:)

  4. DeMi says:

    Hay,

    Here some code using Regular Expressions which makes life a little easier (besides making the regular expression 😉

    public class FTPLineParser

    {

    private Regex unixStyle = new Regex(@&quot;^(?&lt;dir&gt;[-dl])(?&lt;ownerSec&gt;[-r][-w][-x])(?&lt;groupSec&gt;[-r][-w][-x])(?&lt;everyoneSec&gt;[-r][-w][-x])s+(?:d)s+(?&lt;owner&gt;w+)s+(?&lt;group&gt;w+)s+(?&lt;size&gt;d+)s+(?&lt;month&gt;w+)s+(?&lt;day&gt;d{1,2})s+(?&lt;hour&gt;d{1,2}):(?&lt;minutes&gt;d{1,2})s+(?&lt;name&gt;.*)$&quot;);
    
    private Regex winStyle = new Regex(@&quot;^(?&lt;month&gt;d{1,2})-(?&lt;day&gt;d{1,2})-(?&lt;year&gt;d{1,2})s+(?&lt;hour&gt;d{1,2}):(?&lt;minutes&gt;d{1,2})(?&lt;ampm&gt;am|pm)s+(?&lt;dir&gt;[&lt;]dir[&gt;])?s+(?&lt;size&gt;d+)?s+(?&lt;name&gt;.*)$&quot;);
    
    public FTPLineResult Parse(string line)
    
    {
    
        Match match = unixStyle.Match(line);
    
        if (match.Success)
    
        {
    
            return ParseMatch(match.Groups, ListStyle.Unix);
    
        }
    
        match = winStyle.Match(line);
    
        if (match.Success)
    
        {
    
            return ParseMatch(match.Groups, ListStyle.Unix);
    
        }
    
        throw new Exception(&quot;Invalid line format&quot;);
    
    }
    
    private FTPLineResult ParseMatch(GroupCollection matchGroups, ListStyle style)
    
    {
    
        string dirMatch = (style == ListStyle.Unix ? &quot;d&quot; : &quot;&lt;dir&gt;&quot;);
    
        FTPLineResult result = new FTPLineResult();
    
        result.Style = style;
    
        result.IsDirectory = matchGroups[&quot;dir&quot;].Value.Equals(dirMatch, StringComparison.InvariantCultureIgnoreCase);
    
        result.Name = matchGroups[&quot;name&quot;].Value;
    
        if (!result.IsDirectory)
    
            result.Size = long.Parse(matchGroups[&quot;size&quot;].Value);
    
        return result;
    
    }
    

    }

    public enum ListStyle

    {

    Unix,
    
    Windows
    

    }

    public class FTPLineResult

    {

    public ListStyle Style;
    
    public string Name;
    
    public DateTime DateTime;
    
    public bool IsDirectory;
    
    public long Size;
    

    }

  5. DeMi says:

    Hay, DeMi again.

    I hit the <ENTER> to quick when adding my example :-O

    Some details:

    In my previous example its better to use the RegexOptions.IgnoreCase option in the RegEx constructor.

    private Regex unixStyle = new Regex(@"^…$", RegexOptions.IgnoreCase);

    private Regex winStyle = new Regex(@"^…$", RegexOptions.IgnoreCase);

    To call the FTPLineParser you can use the code below:

    //…

    FtpWebResponse ftpResponse = (FtpWebResponse) ftpRequest.GetResponse();

    Stream response = ftpResponse.GetResponseStream();

    StreamReader responseReader = new StreamReader(response);

    string line = null;

    FTPLineParser parser = new FTPLineParser();

    List<FTPLineResult> lines = new List<FTPLineResult>();

    while ((line = responseReader.ReadLine()) != null)

    {

    FTPLineResult lineResult = parser.Parse(line);
    
    lines.Add(lineResult);
    

    }

    DoSomethingWithTheParsedLines(lines);

    Hope this is of some help for you.

    Enjoy.

    Dennis

  6. Bomzhang says:

    Stop! Try to read this interested book:,

  7. Sntzjyhf says:

    Try to look here and may be you find what do you want:,

  8. Kvovydtq says:

    Of course, but what do you think about that?,

  9. DM says:

    Do you know what is refactoring?

  10. Will says:

    Do you have this sample written in VB code?

  11. Stephen says:

    the date.pase() method which parses the concacated date and time was crapping out as my culture was "en-GB", it’s betst to add a private field like

    private CultureInfo _cultureInfo = CultureInfo.CreateSpecificCulture("en-US");

    And then in the line where the date is parsed, it is changed to

    f.CreateTime = DateTime.Parse(dateStr + " " + timeStr, _cultureInfo);

    that way, it will know that it is parsing a US style date.

    great work by the way, this has saved me at least two days of work!

  12. sunss says:

    DeMi’s alternative code seems to work as well (and seems a little neater!).  The only bug I’ve found is changing the 2nd occurence of:

    return ParseMatch(match.Groups, ListStyle.Unix);

    to

    return ParseMatch(match.Groups, ListStyle.Windows);

  13. tynar says:

    Regex for FileZilla Server;

        private Regex fileZilla = new Regex(@"(?<dir>[-dl])(?<ownerSec>)[-r][-w]-x[-r][-w]-x[-r][-w][-x]s+(?:d)s+(?<owner>w+)s+(?<group>w+)s+(?<size>d+)s+(?<month>w+)s+(?<day>d{1,2})s+(?<year>w+)s+(?<name>.*)$");

               match = fileZilla.Match(line);

               if (match.Success)

               {

                   return ParseMatch(match.Groups, ListStyle.Unix);

               }

  14. 家出 says:

    これから家出したい少女や、現在家出中の娘とそんな娘を助けたい人を繋げるSOS掲示板です。10代、20代の女の子が家庭内の問題などでやむなく家出している子が多数書き込みしています。女の子リストを見て彼女たちにアプローチしてみませんか

  15. みんなの精神年齢を測定できる、メンタル年齢チェッカーで秘められた年齢がズバリわかっちゃう!かわいいあの子も実は精神年齢オバサンということも…合コンや話のネタに一度チャレンジしてみよう

  16. 童貞卒業を考えているなら、迷わずココ!今まで童貞とヤッた事がない女性というのは意外と多いものです。そんな彼女たちは一度童貞とやってみたいと考えるのは自然な事と言えるでしょう。当サイトにはそんな好奇心旺盛な女性たちが登録されています

  17. virt says:

    If your ftpsite requires authentication, use

    FtpWebRequest ftpclientRequest = WebRequest.Create(args[0]) as FtpWebRequest;

    ftpclientRequest.Credentials = new NetworkCredential("username", "password");

  18. 即ハメセレブは完全無料でご利用できる出会いコミュニティです。今までにない実績で、あなたの希望に合った人をお探しします。毎月考えられない豪華なイベントを開催しているので出会いを保障します

  19. archae-dev says:

    Thank you, for code and first comment, but how can i know a size file?

  20. Toren Valone says:

    This snippet does not work for me

    FtpWebRequest ftpclientRequest = WebRequest.Create("https://sftp.dts.ca.gov&quot;) as FtpWebRequest;

                   ftpclientRequest.Method = FtpMethods.ListDirectoryDetails;

    This is the error

    Error 14 The name 'FtpMethods' does not exist in the current context C:Documents and SettingsmvtwvMy DocumentsVisual Studio 2005ProjectsFile Generation SystemFile Generation Systemcourt.cs 2339 43 File Generation System

    I have checked all my name spaces and they are fine.

  21. Michael Thyregod says:

    You may have a look at the great opensource lib edtFTPnet from Enterprise Distributed Technologies ( http://www.enterprisedt.com) – they adressed the problem with unix style and when the file is created the current year – and therefore the datestamp is not holding year – besides other things is adressed as well – so for You who need inspiration – look into that great piece og code – really helped me a lot with some simple FTP requests where I only needed a list of files and after that being able to sort the list by date.

  22. Denis says:

    hello i have question ,why  WebRequestMethods.Ftp.ListDirectory; dont show htaccess file ,and how i can solve this,thx

  23. Vineesh Kumar says:

    Adarsh, Thank you so much. It is working perfect for me. Was looking for such kind of an article for a long time.

  24. Cirunz says:

    Great solution. Thanks a lot.

    I added size this way:

    Modified FileStruct:

    public struct FileStruct

       {

           public string Flags;

           public string Owner;

           public string Group;

           public bool IsDirectory;

           public DateTime CreateTime;

           public long Size;

           public string Name;

       }

    In ParseFileStructFromUnixStyleRecord, changed this:

    _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);   //skip one part

    with this:

    f.Size = Convert.ToInt64(_cutSubstringFromStringWithTrim(ref processstr, ' ', 0));

    And in ParseFileStructFromWindowsStyleRecord this:

    string[] strs = processstr.Split(new char[] { ' ' }, true);

    processstr = strs[1].Trim();

    with this:

    f.Size = Convert.ToInt64(processstr.Substring(0, processstr.IndexOf(' ')).Trim());

    processstr = processstr.Remove(0, processstr.IndexOf(' ') + 1);

    (This include the changes proposed by Steffen Xavier xsteffen@ict7.com )

    Just my two cents.