AdvancedHMI Software
General Category => Open Discussion => Topic started by: ddddd13 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
-
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?
-
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
-
Yes the files have a name (always the same no numbers) then 6 digits as follows dd mm yy.
-
I don't have an FT to test this with, but I think this will be a start:
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)
-
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
-
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.
-
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.