Question Check date modified of multiple files on FTP

Coding Camel

Member
Joined
May 14, 2013
Messages
19
Programming Experience
1-3
Hi, i'm new to these forums so sorry if i'm posting in the wrong section.

I am attempting to open a folder in FTP, and check the date modified of ALL files in that folder. I then want to compare that to the date modified of files on my computer. Is there any way I can do this?

I figured out how to get the contents of a folder, and check the date modified of ONE file, but I don't know how to put them together and check multiple files.
 

Coding Camel

Member
Joined
May 14, 2013
Messages
19
Programming Experience
1-3
Automatic File Downloader

Hi, so I just posted about something similar, although ill tell you exactly what i'm trying to do.

So lets say I download a folder from FTP called testFolder, and it has 10 .txt documents inside it. What I want to do is be able to click another button called "update" and go to the ftp server where the original file is located, and download the most recent files. In other words, compare the files on my computer to the ones on the FTP server, and download the ones that have a different modified version than my computer.

Thanks again,
Camel, or whatever I call myself.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,469
Location
Norway
Programming Experience
10+
Based on what you write I don't understand what your problem is, if you can do this for one file you can also to it for another file. Just loop over your local files and for each one do your comparison with the remote file, and if needed download the file.
 

Coding Camel

Member
Joined
May 14, 2013
Messages
19
Programming Experience
1-3
Okay, here's the code i'm using currently. I can get it to loop over and tell me the name of each file, but how would I go about making it tell me the files date modified, and then compare that with a file on my computer.

-EDIT-

I think I can get it to check the file on my computer, but I don't know how i'd get the date modified from the server.

VB.NET:
    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button3.Click        Dim GetFiles As FtpWebRequest
        GetFiles = FtpWebRequest.Create(New Uri("ftp://@hosted14.nfoservers.com/" & "public/"))
        GetFiles.Credentials = New NetworkCredential("user", "pass")
        GetFiles.Method = WebRequestMethods.Ftp.ListDirectory
        Dim sr As StreamReader
        sr = New StreamReader(GetFiles.GetResponse().GetResponseStream())
        Dim aryfiles() As String
        Dim mystring As New Integer
        aryfiles = Split(sr.ReadToEnd, vbNewLine)
        For Each File In aryfiles
            MsgBox(File)
        Next
    End Sub
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,469
Location
Norway
Programming Experience
10+
You said you knew how to check the date modified of ONE file, but it appears you don't. To get ftp file date use WebRequestMethods.Ftp.GetDateTimestamp and read FtpWebResponse.LastModified. For local file use IO.FileInfo class.
 

Coding Camel

Member
Joined
May 14, 2013
Messages
19
Programming Experience
1-3
Thanks for your help, John.

It now checks the date modified of the files on the server in order. But it only does it twice, then gives an error saying it can't find the file. Could this be because it is DIM'ing the file more than once? Or is it cause there seems to be a blank space at the end. If I just do msgBox(file) It goes through all the files, and then at the end there is a blank.

VB.NET:
    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button3.Click        Dim GetFiles As FtpWebRequest
        GetFiles = FtpWebRequest.Create(New Uri("ftp://@hosted14.nfoservers.com/" & "public/"))
        GetFiles.Credentials = New NetworkCredential("someuser", "somepass")
        GetFiles.Method = WebRequestMethods.Ftp.ListDirectory
        Dim sr As StreamReader
        sr = New StreamReader(GetFiles.GetResponse().GetResponseStream())
        Dim aryfiles() As String
        Dim mystring As New Integer
        aryfiles = Split(sr.ReadToEnd, vbNewLine)
        For Each File In aryfiles
            Dim ftp As Net.FtpWebRequest = Net.FtpWebRequest.Create("ftp://someuser:somepass@hosted14.nfoservers.com/public/")
            ftp.Method = Net.WebRequestMethods.Ftp.GetDateTimestamp
            Dim response As FtpWebResponse = DirectCast(ftp.GetResponse(), FtpWebResponse)
            Dim dateModified As DateTime = response.LastModified
            MsgBox(dateModified)
        Next
    End Sub
 
Last edited:

Coding Camel

Member
Joined
May 14, 2013
Messages
19
Programming Experience
1-3
Woops, somehow It got posted wrong. Heres what it looks like. This line : Dim ftp As Net.FtpWebRequest = Net.FtpWebRequest.Create("ftp://someuser:somepass@hosted14.nfoservers.com/public/" & File) should tell it what file. I've tested it, and it works but only for the first two files.

VB.NET:
[COLOR=#333333]    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button3.Click        Dim GetFiles As FtpWebRequest[/COLOR]        GetFiles = FtpWebRequest.Create(New Uri("ftp://@hosted14.nfoservers.com/" & "public/"))
        GetFiles.Credentials = New NetworkCredential("someuser", "somepass")
        GetFiles.Method = WebRequestMethods.Ftp.ListDirectory
        Dim sr As StreamReader
        sr = New StreamReader(GetFiles.GetResponse().GetResponseStream())
        Dim aryfiles() As String
        Dim mystring As New Integer
        aryfiles = Split(sr.ReadToEnd, vbNewLine)
        For Each File In aryfiles
            Dim ftp As Net.FtpWebRequest = Net.FtpWebRequest.Create("ftp://someuser:somepass@hosted14.nfoservers.com/public/" & File)
            ftp.Method = Net.WebRequestMethods.Ftp.GetDateTimestamp
            Dim response As FtpWebResponse = DirectCast(ftp.GetResponse(), FtpWebResponse)
            Dim dateModified As DateTime = response.LastModified
            MsgBox(dateModified)
        Next [COLOR=#333333]    End Sub[/COLOR]
 

Coding Camel

Member
Joined
May 14, 2013
Messages
19
Programming Experience
1-3
Just tested it, it now closes the stream. It seems that after it has displayed all the files, it gets the file not found error. This seems to be caused by what I said the "blank file" that displays if I "file",
for instance

"test.txt"
"test2.txt"
"test3.txt"
"" <--- Message box pops up with nothing in it.
 

Coding Camel

Member
Joined
May 14, 2013
Messages
19
Programming Experience
1-3
It seems to work, only issue is the date modified time on the server is different than that of my computer. I look in windows file browser and it appears the same, but whenever do msgbox(fdate & " " & datemodified) which displays the time on the server and on computer, it is off by 3 hours exactly. Is there any way I can remove the hours, and just get minutes/seconds?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,469
Location
Norway
Programming Experience
10+
Unless it was something about your code, it could be due to a non-standard Ftp server storing and returning its local time, it should use UTC and that is also what FtpWebResponse assumes when GetDateTimestamp is processed, it will convert that value to local time before returning the value to you.
 
Top Bottom