CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: FTp File Access

  1. #1
    Join Date
    Apr 2005
    Posts
    1

    Red face FTp File Access

    I want to get the creation date of a file in ftp site through VB6. Is there any api or member like DateCreated is available? And How to use thaT ONE. Also How to down load a file using proxy.
    Last edited by srivi_senthil; April 20th, 2005 at 05:47 AM.

  2. #2
    Join Date
    Apr 2004
    Posts
    17

    Re: FTp File Access

    hi there,
    take a look at the "Using the Internet Transfer Control" in MSDN.
    It supports HTTP/FTP protocols and you can easily use also a proxy
    server.
    Another possibility is to use the WebBrowser/Scriptlet MS controls and
    set manually in the registry the proxy values (proxy server and port)
    (this is because WebBrowser/Scriptlet control uses the IE settings).

    regards,
    typecast

    p.s. if you are interested about the second possibility i can give
    you a code example next time in my post ...
    bye

  3. #3
    Join Date
    Dec 2001
    Posts
    6,332

    Re: FTp File Access

    You may want to take a look at some examples on sites like PlanetSourceCode.com. That should provide you with a lot of insight on various methods to accomplish what you want to do.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  4. #4
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: FTp File Access

    You may use also the WinInet. Here is a sample code from API-Guide. It may be of great help for you.

    Code:
    Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
    Const FTP_TRANSFER_TYPE_ASCII = &H1
    Const FTP_TRANSFER_TYPE_BINARY = &H2
    Const INTERNET_DEFAULT_FTP_PORT = 21               ' default for FTP servers
    Const INTERNET_SERVICE_FTP = 1
    Const INTERNET_FLAG_PASSIVE = &H8000000            ' used for FTP connections
    Const INTERNET_OPEN_TYPE_PRECONFIG = 0                    ' use registry configuration
    Const INTERNET_OPEN_TYPE_DIRECT = 1                        ' direct to net
    Const INTERNET_OPEN_TYPE_PROXY = 3                         ' via named proxy
    Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4   ' prevent using java/script/INS
    Const MAX_PATH = 260
    Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
    Private 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
    Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
    Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession 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
    Private 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
    Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
    Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
    Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
    Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
    Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
    Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
    Private 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
    Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
    Const PassiveConnection As Boolean = True
    Private Sub Form_Load()
        'KPD-Team 2000
        'URL: http://www.allapi.net
        'E-Mail: [email protected]
        Dim hConnection As Long, hOpen As Long, sOrgPath  As String
        'open an internet connection
        hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
        'connect to the FTP server
        hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT, "your login", "your password", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
        'create a buffer to store the original directory
        sOrgPath = String(MAX_PATH, 0)
        'get the directory
        FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
        'create a new directory 'testing'
        FtpCreateDirectory hConnection, "testing"
        'set the current directory to 'root/testing'
        FtpSetCurrentDirectory hConnection, "testing"
        'upload the file 'test.htm'
        FtpPutFile hConnection, "C:\test.htm", "test.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0
        'rename 'test.htm' to 'apiguide.htm'
        FtpRenameFile hConnection, "test.htm", "apiguide.htm"
        'enumerate the file list from the current directory ('root/testing')
        EnumFiles hConnection
        'retrieve the file from the FTP server
        FtpGetFile hConnection, "apiguide.htm", "c:\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
        'delete the file from the FTP server
        FtpDeleteFile hConnection, "apiguide.htm"
        'set the current directory back to the root
        FtpSetCurrentDirectory hConnection, sOrgPath
        'remove the direcrtory 'testing'
        FtpRemoveDirectory hConnection, "testing"
        'close the FTP connection
        InternetCloseHandle hConnection
        'close the internet connection
        InternetCloseHandle hOpen
    End Sub
    Public Sub EnumFiles(hConnection As Long)
        Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
        'set the graphics mode to persistent
        Me.AutoRedraw = True
        'create a buffer
        pData.cFileName = String(MAX_PATH, 0)
        'find the first file
        hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
        'if there's no file, then exit sub
        If hFind = 0 Then Exit Sub
        'show the filename
        Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
        Do
            'create a buffer
            pData.cFileName = String(MAX_PATH, 0)
            'find the next file
            lRet = InternetFindNextFile(hFind, pData)
            'if there's no next file, exit do
            If lRet = 0 Then Exit Do
            'show the filename
            Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
        Loop
        'close the search handle
        InternetCloseHandle hFind
    End Sub
    Sub ShowError()
        Dim lErr As Long, sErr As String, lenBuf As Long
        'get the required buffer size
        InternetGetLastResponseInfo lErr, sErr, lenBuf
        'create a buffer
        sErr = String(lenBuf, 0)
        'retrieve the last respons info
        InternetGetLastResponseInfo lErr, sErr, lenBuf
        'show the last response info
        MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
    End Sub
    Busy

  5. #5
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: FTp File Access

    The WIN32_FIND_DATA would give you the date in file format. To convert it into a system time you will need another API function which is the FileTimeToSystemTime. Once again, below is a sample code from API-Guide.

    Code:
    'This program needs a Dialog box, named CDBox1
    '  (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
    '   and select Microsoft Common Dialog control)
    Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
    Private Type SHFILEOPSTRUCT
        hWnd As Long
        wFunc As Long
        pFrom As String
        pTo As String
        fFlags As Integer
        fAborted As Boolean
        hNameMaps As Long
        sProgress As String
    End Type
    Private 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
    Private Const GENERIC_WRITE = &H40000000
    Private Const OPEN_EXISTING = 3
    Private Const FILE_SHARE_READ = &H1
    Private Const FILE_SHARE_WRITE = &H2
    Private Const FO_DELETE = &H3
    Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
    Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As Long) As Long
    Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
    Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
    Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
    Private Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
    Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Dim lngHandle As Long, SHDirOp As SHFILEOPSTRUCT, lngLong As Long
        Dim Ft1 As FILETIME, Ft2 As FILETIME, SysTime As SYSTEMTIME
        'Set the dialog's title
        CDBox.DialogTitle = "Choose a file ..."
        'Raise an error when the user pressed cancel
        CDBox.CancelError = True
        'Show the 'Open File'-dialog
        CDBox.ShowOpen
        'Create a new directory
        CreateDirectory "C:\KPD-Team", ByVal &H0
        'Copy the selected file to our new directory
        CopyFile CDBox.filename, "C:\KPD-Team\" + CDBox.FileTitle, 0
        'Rename the file
        MoveFile "C:\KPD-Team\" + CDBox.FileTitle, "C:\KPD-Team\test.kpd"
        'Open the file
        lngHandle = CreateFile("C:\KPD-Team\test.kpd", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
        'Get the file's size
        MsgBox "The size of the selected file is" + Str$(GetFileSize(lngHandle, lngLong)) + " bytes."
        'Get the fil's time
        GetFileTime lngHandle, Ft1, Ft1, Ft2
        'Convert the file time to the local file time
        FileTimeToLocalFileTime Ft2, Ft1
        'Convert the file time to system file time
        FileTimeToSystemTime Ft1, SysTime
        MsgBox "The selected file was created on" + Str$(SysTime.wMonth) + "/" + LTrim(Str$(SysTime.wDay)) + "/" + LTrim(Str$(SysTime.wYear))
        'Close the file
        CloseHandle lngHandle
        'Delete the file
        DeleteFile "C:\KPD-Team\test.kpd"
        With SHDirOp
            .wFunc = FO_DELETE
            .pFrom = "C:\KPD-Team"
        End With
        'Delete the directory
        SHFileOperation SHDirOp
        End
    End Sub
    Goodluck!
    Busy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured