C# Class to Parse Webrequestmethods.Ftp.Listdirectorydetails Ftp Response

C# class to parse WebRequestMethods.Ftp.ListDirectoryDetails FTP response

One solution I came across is EdtFTPnet

EdtFTPnet seems to be quite a feature packed solution that handles lots of different FTP options so is ideal.

It's the free open source solution that I've how employed for http://www.ftp2rss.com (a little tool I needed myself but figured might be useful to others also).

Parsing FtpWebRequest ListDirectoryDetails line

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

How to parse this? ftpWebRequest ListDirectorDetials


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"
}

No year being returned using ListDirectoryDetails C#

I've found out that Unix doesn't gives year unless the file is from the previous year and for the current year it gives time. So get around this i've used the following code:

if (unParsedCreateTime.Contains(":"))
{
var x = Convert.ToDateTime(DateTime.Now.Year.ToString(CultureInfo.InvariantCulture) + " " + unParsedCreateTime);
}
else
{
var x = Convert.ToDateTime(unParsedCreateTime);
}

ftp web response date time format

No repro.

That regex does capture the timestamps. The following code :

var input=@"01-27-17  06:54AM                14613 A J DOHMANN CHRYSLER INC.csv
09-20-18 12:27PM 122816576 ABC1Append.csv
09-12-18 08:45AM 54998269 ABC1_FileForAppend.csv";

var pattern="(?<timestamp>\\d{2}\\-\\d{2}\\-\\d{2}\\s+\\d{2}:\\d{2}[Aa|Pp][mM])\\s+(?<dir>\\<\\w+\\>){0,1}(?<size>\\d+){0,1}\\s+(?<name>.+)";
var rex=new Regex(pattern);
var ts=rex.Match(input).Groups["timestamp"].Value;

Returns

01-27-17  06:54AM

I suspect some other code is trying to parse this string, fails and returns a default DateTime value.

This string can be parsed eg with :

DateTime.Parse(ts,CultureInfo.GetCultureInfo("en-US"));

or

if(DateTime.TryParse(ts,CultureInfo.GetCultureInfo("en-US"),DateTimeStyles.None,out var dt))
{
Console.WriteLine(dt);
}
else
{
//Parsing failed!
}

Count/list all files that are in FTP directory with FtpWebRequest in C#

Your code works for me, but @Ozug can be right that your server fails to use CRLF line endings.

A more robust (and more efficient too) implementation is:

List<string> names = new List<string>();
while (!reader.EndOfStream)
{
names.Add(reader.ReadLine());
}

It should handle even CR line endings.

See also C# class to parse WebRequestMethods.Ftp.ListDirectoryDetails FTP response.



Related Topics



Leave a reply



Submit