0

I have tried with Ftp Web Response to access files and directory details in c# with ASP.Net.

System.Net.WebRequestMethods.Ftp.ListDirectoryDetails

I got the response like below from FTP server:

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

When i tried to format the date time with below regular expression, i getting the result as "01-01-0001 12:00 AM"

regular expression:

(?<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>.+)

Please some one help in this, how to get proper date time format from FTP web response.

TIA.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Balaji V
  • 3
  • 2
  • @TheIncorrigible1 an FTP server's response *is* text – Panagiotis Kanavos Feb 20 '19 at 15:49
  • @TheIncorrigible1 that's not the case. And FTP returns *text listings* that have to be parsed. That's how FTP works. FtpWebRequest doesn't offer a lot of FTP functionality either. There are other FTP libraries but even those have to *parse* the results – Panagiotis Kanavos Feb 20 '19 at 15:51
  • @TheIncorrigible1 check [How to: List directory contents with FTP](https://learn.microsoft.com/en-us/dotnet/framework/network-programming/how-to-list-directory-contents-with-ftp) – Panagiotis Kanavos Feb 20 '19 at 15:52
  • No repro anyway. First, I *do* get all the data. Second, a *regex* will only return the text it finds. No text, no result. It wouldn't return `01-01-0001 12:00 AM`. Looks like some *other* code tries to parse the timestamp and fails, returning the default DateTime object whose value is January 1, 0001 – Panagiotis Kanavos Feb 20 '19 at 15:55
  • How are you parsing the timestamp value? Did you use `TryParse` or `TryParseExact`? Did you *check* their response to see whether parsing succeded? – Panagiotis Kanavos Feb 20 '19 at 15:56

1 Answers1

0

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!
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Thanks, its worked for me. But i only getting "27-Jan-17 6:54:00 AM". How i get full year just like "27-Jan-2017 6:54:00 AM". Any possible? TIA – Balaji V Feb 21 '19 at 06:38
  • @BalajiV Parse the string and get the actual DateTime out of it. You can format the DateTime any way you want after that – Panagiotis Kanavos Feb 21 '19 at 07:53