Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a program that fetches the file creation time from web server using FTP ListDirectoryDetails method. I want to store the date and time when the file is created in database. But the problem is that the format which is coming from web server is like

"-rw-r--r-- 1 ftp ftp 9134 Mar 19 03:38 filename"

how i will fetch the date and time from the string please help.
Posted

Try this function


C#
private static DateTime? GetTime(string s)
{
    var regex = new Regex(
                    @"^[\-drswx]{10}\s+\d+\s+ftp\s+ftp\s+\d+\s+" +
                    @"(?<Month>[a-zA-Z]{3})\s+(?<Day>\d{1,2})\s+" +
                    @"(?<Hours>\d{2})\:(?<Mins>\d{2})\s+.*$");

    var m = regex.Match(s);

    DateTime? result = null;

    if (m.Success)
    {
        var year = DateTime.Now.Year.ToString();
        var month = m.Groups["Month"].ToString();
        var day = m.Groups["Day"].ToString();
        var hours = m.Groups["Hours"].ToString();
        var mins = m.Groups["Mins"].ToString();

        var timepart = string.Format(@"{0}/{1}/{2} {3}:{4}", year, month, day, hours, mins);

        result = DateTime.ParseExact(timepart, "yyyy/MMM/dd HH:mm"
                , new CultureInfo("en-US"));

        if (result.Value.Month > DateTime.Now.Month)
        {
            result.Value.AddYears(-1);
        }
    }
    else
    {
        regex = new Regex(
                    @"^[\-drswx]{10}\s+\d+\s+ftp\s+ftp\s+\d+\s+" +
                    @"(?<Month>[a-zA-Z]{3})\s+(?<Day>\d{1,2})\s+" +
                    @"(?<Year>\d{4})\s+.*$");
            
        m = regex.Match(s);

        if (!m.Success)
        {
            return null;
        }

        var year = m.Groups["Year"].ToString();
        var month = m.Groups["Month"].ToString();
        var day = m.Groups["Day"].ToString();

        var timepart = string.Format(@"{0}/{1}/{2}", year, month, day);

        result = DateTime.ParseExact(timepart, "yyyy/MMM/dd", new CultureInfo("en-US"));
    }
    return result;
}
 
Share this answer
 
v8
Hi,


Please refer the links given below:

FTP ListDirectoryDetails not returning data/time consistently

FTP – Get directory listing




Don't forget to mark useful responses as Answer if they helped you towards a solution. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900