Search code examples
c#.netftpftpwebrequest

Cannot use GetDateTimestamp for FTP folder


  • I have to copy files to a particular directory. Before copying the files, I have to check if the directory already exists (if not then, create one).

  • To check the existence of a directory, I try to get the timestamp of that directory. If the directory exists then, I will get its timestamp and if not, then I will create a new directory.

My code to receive timestamp

// Try to get the LastModified date of the folder whose existence has to be checked

// Get the object used to communicate with the server.  
string DirectoryPath = "ftp://66.220.9.50/FileDirectory";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(DirectoryPath));
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
request.Credentials = new NetworkCredential(_username, _password);

//Step-1: This line will decide if the Directory exists or not
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Status-1: " + response.StatusDescription);
Console.WriteLine("Last Modified: " + response.LastModified);

validDirectory = true;
response.Close();

PROBLEM: The above code works fine if I use another Windows PC as an FTP server (using FileZilla). But if I try to get the timestamp using above code from an online FTP server (wwww.driveHQ.com) then, the line FtpWebResponse response = (FtpWebResponse)request.GetResponse(); throws an exception:

The remote server returned an error: <550> File unavailable (e.g. file not found, no access)

PS: I am able to connect to the server (got status code: 150, Connection Accepted). My Uri is also correct (I am successfully able to create a directory). The problem comes only if I try to get the timestamp of this created directory OR try to get a list of files inside the directory.


Solution

  • The GetDateTimestamp method uses FTP MDTM command underneath.

    Many FTP servers, including IIS or the DriveHQ, do not support the MDTM command for folders.


    Other way to retrieve modification time is the MLST command. But that's not supported by the FtpWebRequest. You would have to use a different FTP client library (like my WinSCP .NET assembly and its Session.GetFileInfo method).

    But that won't help you much either. Many servers do not support the MLST command at all (e.g. IIS). And the DriveHQ returns malformed (imho) response to the MLST command. While it contains the modification time, it does not contain file name and WinSCP fails to parse the response. You would have to do some gross hack, like parsing WinSCP session log file to get the modification timestamp out of it. Or maybe another 3rd party library will be able to cope with the DriveHQ response.


    The last option is to do a complete listing of the parent directory, retrieving the timestamp of the subdirectory from the listing.

    While that's not good solution is general, as the FtpWebRequest supports only LIST command, what does not have standard format, the DriveHQ uses relatively standard *nix format, so you can for example use my answer to Parsing FtpWebRequest ListDirectoryDetails line.


    Though as you actually use the GetDateTimestamp only as a check for folder existence, you can simply use the ListDirectory method instead of GetDateTimestamp. It's obviously an overkill, but it's by far the easiest solution with combined limitations of the FtpWebRequest and DriveHQ.

    See How to check if an FTP Directory Exists.