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:
- object type:
- d : directory
- - : file
- Array[3] of permissions (rwx-)
- File Size
- Last Modified Date
- Last Modified Time
- 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
How to Get Current User in ASP.NET Core
Servicestack Request Dto Design
Detect Target Framework Version at Compile Time
Format Xml String to Print Friendly Xml String
Web Reference VS. Service Reference
Attach a File from Memorystream to a Mailmessage in C#
How to Clone/Deep Copy a .Net Generic Dictionary<String, T>
Validate Image from File in C#
Check for Column Name in a SQLdatareader Object
Generating Permutations of a Set (Most Efficiently)
Populating a Razor Dropdownlist from a List<Object> in MVC
How to Restart My C# Winform Application
Adding Placeholder Text to Textbox
Most Efficient Way to Test Equality of Lambda Expressions
Referencing a Variable from Another Method
How to Run an Exe File from My C# Code