Author Topic: Loading Newest File  (Read 904 times)

ddddd13

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Loading Newest File
« on: June 04, 2019, 04:35:29 PM »
I am looking for code to find the newest file using FTP.

Anything would be appreciated,
Dave

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5269
    • View Profile
    • AdvancedHMI
Re: Loading Newest File
« Reply #1 on: June 04, 2019, 04:57:40 PM »
It doesn't look like FTP gives the date of the file when fetching the directory listing. So I think it would be necessary to get the file list then using the LastModified command one at a time on all files. This could be slow on a directory with a lot of files.

Do the files have any kind of date in their name that could be used?

ddddd13

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Loading Newest File
« Reply #2 on: June 04, 2019, 05:28:55 PM »
I'm not sure what you mean by the date but the code

 Dim request As Net.FtpWebRequest = DirectCast(WebRequest.Create(MainForm.FtpAddress & "*.*"), FtpWebRequest)
       
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails

will return the file with the  date

ddddd13

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Loading Newest File
« Reply #3 on: June 04, 2019, 05:31:06 PM »
Yes the files have a name (always the same no numbers) then 6 digits as follows dd mm yy.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5269
    • View Profile
    • AdvancedHMI
Re: Loading Newest File
« Reply #4 on: June 04, 2019, 05:41:01 PM »
I don't have an FT to test this with, but I think this will be a start:
Code: [Select]
        Dim request As FtpWebRequest = CType(WebRequest.Create("ftp://www.server.com/"), FtpWebRequest)
        request.Method = WebRequestMethods.Ftp.ListDirectory
        request.Credentials = New NetworkCredential("username", "password")

        Dim response As FtpWebResponse = CType(request.GetResponse, FtpWebResponse)
        Dim responseStream As System.IO.Stream = response.GetResponseStream
        Dim reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)
        Dim names As String = reader.ReadToEnd
        reader.Close()
        response.Close()

        Dim FileList() As String = names.Split(New Char() {Chr(13), Chr(10)}, StringSplitOptions.RemoveEmptyEntries)
        Array.Sort(FileList)

        Dim NewestFile As String = FileList(0) '*May be  FileList(FileList.Length-1)

ddddd13

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Loading Newest File
« Reply #5 on: June 05, 2019, 09:53:13 AM »
As not a lot of these are generated. I think I would be better to read the file then erase it or send it to another directory, then check the directory for a file.

The problem with that approach is the computer may go down and the directory could have more than one file. This could be solved by loading a file of the current hour of the day. Remember the file structure
"filename-ddmmyyhhmmss-000000000.dat" I would like to solve this by a wild card something like the following.
filename-06051908*.*
however the ftp does not recognize the wildcard.
Any ideas?
Dave

ddddd13

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Loading Newest File
« Reply #6 on: June 05, 2019, 11:01:49 AM »
I get the following alarm.
Exception thrown: 'System.Net.WebException' in System.dll
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
I'm not sure if that means System.Net could not handle the wildcard or if the remote server could not.

ddddd13

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Loading Newest File
« Reply #7 on: June 05, 2019, 01:46:20 PM »
I got the wildcards to work. When doing a directory list it shows what I mistakenly took for the filename then -ddmmyyhhmmss. In reality the file name is ddmmyyhhmmss the first part is the directory. Now it will be easy.