0

I want to download the oldest file from the directory on the FTP server.

I am connecting to the FTP server using FtpWebrequest to get the list of files in the directory. I use WebRequestMethods.Ftp.ListDirectoryDetails to do so. Once I know the file name, I'll create another FTP connection to download that file.

However, the details I get in the FtpWebResponse stream are in different format when FTP is on Linux and when it's on the Windows.

  1. How to deal with this problem?
  2. I can write different methods for Linux and Windows that parse the response stream. But for that, at least the way Linux Ftp and Windows FTP provides details should be definite. Do you know if the way details are sent may vary from one Linux FTP server to another?
  3. I observed (at least on the Windows FTP) that the oldest file is listed at the end of the response stream every time. And the file name is at the end of every line. Can I take it for granted and write a code?
  4. Can you suggest smarter way of getting the oldest file details?
Learner
  • 4,661
  • 9
  • 56
  • 102

1 Answers1

0

A format/pattern in which FTP servers send response is dependent on how the server is created/coded. So the format of the response stream of ListDirectoryDetails will vary from one Linux/Windows server to another Linux/Windows server.

And from various sources I came to know that there exists around 400 formats in which Linux FTP can respond to the ListDirectoryDetails.

So you cannot rely on or assume any one pattern.

There are couple of solutions to this:

  1. Use the ListDirectory command first. It'll give you contents (directory records) of the directory. Then for every content/directory record, use GetDateTimestamp. It'll give you date time stamp on all the files and you can then decide which is the oldest file. But this solution is not efficient since you have to issue FtpWebRequest for every file just to know the Datetimestamp. (n+1 number of calls if there are 'n' files.)

  2. Another solution is to use the ListDirectoryDetails only. This does not solve the entire problem programatically.

    You can assume most frequently used response pattern and code against it. And you can document this assumption in documents like User Guide or Configuration Guide. It certainly limits your solution to one pattern each for Windows and Linux, but it should be fairly acceptable. (It was acceptable in my case at least). I think it's reasonable to code against most frequently encountered patterns instead of writing huge and clumsy code that tries to handle all possible patterns (and that too when you are not sure if there could be one more pattern!).

    You can use regular expression to match the response pattern and if does not match, you can take appropriate action like throwing some exception etc. Another benefit is that you would get date time stamp for 'n' number of files in just 1 call (compare it against solution #1). And it would be definitely faster to decide the oldest date time stamp in .NET code instead of calling FTP server n+1 times.

Learner
  • 4,661
  • 9
  • 56
  • 102