Solved

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

Posted on 2002-07-15
6
152 Views
Last Modified: 2010-05-02
how can i get the date of a file on a ftp server?
0
Comment
Question by:Jambyte
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:AlonHirsch
Comment Utility
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
 
LVL 4

Accepted Solution

by:
Glowman earned 100 total points
Comment Utility
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
 
LVL 2

Author Comment

by:Jambyte
Comment Utility
when i try to call the Download sub i get an error highlighting MAX_PATH and saying: Compile error: Constatnt Expression required
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Expert Comment

by:Glowman
Comment Utility
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
 
LVL 2

Author Comment

by:Jambyte
Comment Utility
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
 
LVL 2

Author Comment

by:Jambyte
Comment Utility
oops sorry this is the date checking code nevermind... and thank you!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now