Can't use GetDateTimestamp for FTP folder

  • I need to copy files to a specific directory. Before copying the files, I have to check if the directory exists (if not, create it).

  • To check for the existence of a directory, I am trying to get the timestamp of that directory. If the directory exists, then I'll get its timestamp, and if not, I'll create a new directory.

My code for getting 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.  
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;


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

throws an exception:

The remote server returned an error: <550> File not available (for example, file not found, no access)

PS: I can connect to the server (got status code: 150, Connection Accepted). Mine is Uri

also correct (I can create directory successfully). The problem only occurs if I try to get the timestamp of this created directory, or try to get a list of files inside a directory.


source to share

1 answer

The method GetDateTimestamp

uses the FTP command MDTM


Many FTP servers, including IIS or DriveHQ, do not support the MDTM

folder command .

Another way to get the modification time is with a command MLST

. But this is not supported FtpWebRequest

. You will need to use another FTP client library (like WinSCP.NET assembly and its Session.GetFileInfo


But that won't help either. Many servers don't support the command at all MLST

(like IIS). And DriveHQ returns a wrong response (imho) to the command MLST

. Although it contains the modification time, it does not contain a filename, and WinSCP does not parse the response. You will need to do a crude hack, such as splitting the WinSCP session log file to get the timestamp of the change from it. Or perhaps another third party library can handle DriveHQ's answer.

The last option is to make a complete listing of the parent directory by extracting the subdirectory timestamp from the list.

While this is not a good solution is general since FtpWebRequest

it only supports command LIST

, which is not a standard format, DriveHQ uses a relatively standard * nix format, so you can, for example, use my answer to Parsing a FtpWebRequest ListDirectoryDetails string .

Although, since you are actually GetDateTimestamp

only using it as a check for the existence of a folder, you can simply use the method ListDirectory

instead GetDateTimestamp

. This is clearly overkill, but it is by far the simplest solution with combined constraints FtpWebRequest

and DriveHQ.

See How to check if an FTP directory exists .



All Articles