5

I am playing around with FtpWebRequest and I am wondering how can I format the result?

    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("");
        ftp.Credentials = new NetworkCredential("", "");
       ftp.KeepAlive = true;
       ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
       WebResponse response = ftp.GetResponse();
       StreamReader reader = new StreamReader(response
                                       .GetResponseStream());

       string r = reader.ReadLine();
       response.Close();
       reader.Close();

I get results like this back

09-17-11  01:00AM               942038 my.zip

What would be a good way to parse this into like an object say something like

public Class Test()
{
   public DateTime DateCreated? {get; set;}
   public int/long  Size {get; set;}
   public string  Name {get; set;}
}

Not sure if I should use a long or int for the size. I am also not sure what the datetime is actually if it is created, or modified or whatever.

chobo2
  • 83,322
  • 195
  • 530
  • 832
  • 1
    http://stackoverflow.com/questions/7060983/c-class-to-parse-webrequestmethods-ftp-listdirectorydetails-ftp-response – meziantou Sep 19 '11 at 20:01

3 Answers3

8
var value = "09-17-11  01:00AM               942038 my.zip";
var tokens = value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length > 3)
{
    var test = new Test
    {
        DateCreated = DateTime.ParseExact(tokens[0] + tokens[1], "MM-dd-yyHH:mmtt", CultureInfo.InvariantCulture),
        Size = int.Parse(tokens[2]),
        Name = tokens[3]
    };

    // at this stage:
    // test.DateCreated = 17/09/2011 01:00AM
    // test.Size = 942038
    // test.Name = "my.zip"
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Ah. No wonder why it did not work for me. I just did ''. Was wondering why it was not splitting. Not sure if this is the best way to go though as this seems to be something that will need to constantly be fixed(as if I move it to another service they might have more fields) – chobo2 Sep 19 '11 at 20:20
  • @chobo2, you could always use a third party control to do the parsing and which will work in all case (where of course you will have to define what *all cases* means). I've only showed a crude example using standard BCL classes that of course will work only for this specific output. – Darin Dimitrov Sep 19 '11 at 20:41
  • Ya that's what I am looking for now. I am trying to find a free one. I tried almost all on nuget but they are either pay or don't have directoryListing with filesizes – chobo2 Sep 19 '11 at 20:43
1

An annoying thing about the FTP standard is that it does not specify exactly how the directory listing should be formatted. In general, listings returned from *nix machines look more like *nix directory listings and those returned from Windows often look a lot like a DOS listing but you've got old FTP code forming the base of newer products so there is IBM-4690 and AS400 stuff, VMS, Oracle, Novell and so on.

So if you are trying to make something general purpose instead of for a specific server then you've got a lot of ugly parsing work to do. It might be worth your time to buy something but I don't have any recommendations.

AlexPace
  • 143
  • 3
0

This is my algorithm for parsing ListDirectoryDetails. I separated the File/Dir name, Attribute, Date Created, and Size into List. Hope this helps....

        FtpWebRequest _fwr = FtpWebRequest.Create(uri) as FtpWebRequest;
        _fwr.Credentials = cred;
        _fwr.UseBinary = true;
        _fwr.UsePassive = true;
        _fwr.KeepAlive = true;
        _fwr.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        StreamReader _sr = new StreamReader(_fwr.GetResponse().GetResponseStream());

        List<object> _dirlist = new List<object>();
        List<object> _attlist = new List<object>();
        List<object> _datelist = new List<object>();
        List<long> _szlist = new List<long>();
        while (!_sr.EndOfStream)
        {
            string[] buf = _sr.ReadLine().Split(' ');
            //string Att, Dir;
            int numcnt = 0, offset = 4; ;
            long sz = 0;
            for (int i = 0; i < buf.Length; i++)
            {
                //Count the number value markers, first before the ftp markers and second
                //the file size.
                if (long.TryParse(buf[i], out sz)) numcnt++;
                if (numcnt == 2)
                {
                    //Get the attribute
                    string cbuf = "", dbuf = "", abuf = "";
                    if (buf[0][0] == 'd') abuf = "Dir"; else abuf = "File";
                    //Get the Date
                    if (!buf[i+3].Contains(':')) offset++;
                    for (int j = i + 1; j < i + offset; j++)
                    {
                        dbuf += buf[j];
                        if (j < buf.Length - 1) dbuf += " ";
                    }
                    //Get the File/Dir name
                    for (int j = i + offset; j < buf.Length; j++)
                    {
                        cbuf += buf[j];
                        if (j < buf.Length - 1) cbuf += " ";
                    }
                    //Store to a list.
                    _dirlist.Add(cbuf);
                    _attlist.Add(abuf);
                    _datelist.Add(dbuf);
                    _szlist.Add(sz);

                    offset = 0;
                    break;
                }
            }
        }
XCoder8
  • 11
  • 1