how can i get the date of a file on a ftp server?

how can i get the date of a file on a ftp server?
LVL 2
JambyteAsked:
Who is Participating?
 
GlowmanConnect With a Mentor Commented:
Public Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * MAX_PATH
        cAlternate As String * 14
End Type

Public Type SYSTEMTIME
        wYear As Integer
        wMonth As Integer
        wDayOfWeek As Integer
        wDay As Integer
        wHour As Integer
        wMinute As Integer
        wSecond As Integer
        wMilliseconds As Integer
End Type



Public Sub Download()
       
    Dim hOpen As Long
    Dim FileName(10) As String
    Dim FileSize(10) As Double
    Dim FileDate(10) As String
    Dim DownLoads(10) As String
    Dim DownloadDate(10) As String
    Dim DownloadSize(10) As String
    Dim TotalFiles As Integer
    Dim pData As WIN32_FIND_DATA
    Dim hFind As Long
    Dim nLastError As Long
    Dim x As Integer
    Dim y As Integer
    Dim bRet As Boolean
    Dim strDate As SYSTEMTIME
    Dim Holder
    Dim hFile As Long
   
    'Connect to the File Server
    hOpen = InternetOpen("ftp Generic", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
   
    hConnect = InternetConnect(hOpen, Url, 0, FtpId & "@" & ProxyId & "@" & Url, FtpPwd & "@" & ProxyPwd, 1, INTERNET_FLAG_PASSIVE, 0)
   
    If hConnect = 0 Then
        MsgBox "You were unable to connect to the Server : " & Url, vbCritical, "Internet Connection"
        Exit Sub
    End If
   
    'Enumerate the Directory putting values into the File* Array
    pData.cFileName = String(MAX_PATH, 0)
    hFind = FtpFindFirstFile(hConnect, "*.*", pData, 0, 0)
   
    If hFind = 0 Then
        If nLastError = ERROR_NO_MORE_FILES Then
            MsgBox "This directory is empty!", vbInformation, " Site is Empty"
            Exit Sub
        End If
    End If
   
    FileName(x) = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
    FileSize(x) = pData.nFileSizeLow
    Holder = FileTimeToSystemTime(pData.ftLastWriteTime, strDate)
    FileDate(x) = strDate.wMonth & "-" & strDate.wDay & "-" & strDate.wYear
   
    x = 1
   
    Do While x < 10
        pData.cFileName = String(MAX_PATH, 0)
        bRet = InternetFindNextFile(hFind, pData)
        If bRet = False Then
            nLastError = Err.LastDllError
            If nLastError = ERROR_NO_MORE_FILES Then
                Exit Do
            Else
                TotalFiles = x
                Exit Sub
            End If
        Else
            FileName(x) = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
            FileSize(x) = pData.nFileSizeLow
            Holder = FileTimeToSystemTime(pData.ftLastWriteTime, strDate)
            FileDate(x) = strDate.wMonth & "-" & strDate.wDay & "-" & strDate.wYear
            x = x + 1
        End If
        DoEvents
    Loop
   
    'Close the handle
    CloseHandle (hFind)
   
    TotalFiles = x

End Sub


This is how I do it.  I have all of the Wininet function declarations if you need some more info.  Let me know if it helps or if you need more.

G
0
 
Alon HirschSoftware Development ManagerCommented:
Hi,

When you send an 'ls' query to an FTP site - you get the file name, date and size returned.
simply parse the data returned from the command and pick the date from that.

You will have to experiment a bit first.

HTH,
Alon
0
 
JambyteAuthor Commented:
when i try to call the Download sub i get an error highlighting MAX_PATH and saying: Compile error: Constatnt Expression required
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
GlowmanCommented:
Jambyte,

There are a few constants and a couple declerations that you need as well.  I will try to list them all here.

'/////// Constants
Public Const MAX_PATH = 260
Public Const ERROR_NO_MORE_FILES = 18&
Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Public Const INTERNET_FLAG_PASSIVE = &H8000000

'////// Wininet.dll
Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hOpen As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Public Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
Public Declare Function FtpOpenFile Lib "wininet.dll" Alias "FtpOpenFileA" (ByVal hFtpSession As Long, ByVal sFileName As String, ByVal lAccess As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, pBuffer As Byte, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer

That should be about all you need.

G
0
 
JambyteAuthor Commented:
when it saves the file it uses the clients date to stamp the file with. is there a way i can make it use the server's file date? i uped the points a little cuz this gets tricker
0
 
JambyteAuthor Commented:
oops sorry this is the date checking code nevermind... and thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.