Results 1 to 3 of 3

Thread: FTP question

  1. #1

    Thread Starter
    Hyperactive Member Dmitri K's Avatar
    Join Date
    Sep 2002
    Location
    West Palm Beach, FL
    Posts
    444

    FTP question

    Does anyone know how to download/upload a single text file from/to an FTP server without using INET or any other OCX control? Can it be done with APIs?

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    this is the best place to search.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    It looks like you can use APIs to do it. Here is an example from allapi.net
    VB Code:
    1. Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
    2. Const FTP_TRANSFER_TYPE_ASCII = &H1
    3. Const FTP_TRANSFER_TYPE_BINARY = &H2
    4. Const INTERNET_DEFAULT_FTP_PORT = 21               ' default for FTP servers
    5. Const INTERNET_SERVICE_FTP = 1
    6. Const INTERNET_FLAG_PASSIVE = &H8000000            ' used for FTP connections
    7. Const INTERNET_OPEN_TYPE_PRECONFIG = 0                    ' use registry configuration
    8. Const INTERNET_OPEN_TYPE_DIRECT = 1                        ' direct to net
    9. Const INTERNET_OPEN_TYPE_PROXY = 3                         ' via named proxy
    10. Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4   ' prevent using java/script/INS
    11. Const MAX_PATH = 260
    12. Private Type FILETIME
    13.     dwLowDateTime As Long
    14.     dwHighDateTime As Long
    15. End Type
    16. Private Type WIN32_FIND_DATA
    17.     dwFileAttributes As Long
    18.     ftCreationTime As FILETIME
    19.     ftLastAccessTime As FILETIME
    20.     ftLastWriteTime As FILETIME
    21.     nFileSizeHigh As Long
    22.     nFileSizeLow As Long
    23.     dwReserved0 As Long
    24.     dwReserved1 As Long
    25.     cFileName As String * MAX_PATH
    26.     cAlternate As String * 14
    27. End Type
    28. Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet As Long) As Long
    29. Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal _
    30. sServerName As String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal sPassword As String, ByVal lService _
    31. As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
    32. Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType _
    33. As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    34. Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, _
    35. ByVal lpszDirectory As String) As Boolean
    36. Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, _
    37. ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
    38. Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal _
    39. lpszDirectory As String) As Boolean
    40. Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal _
    41. lpszDirectory As String) As Boolean
    42. Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal _
    43. lpszFileName As String) As Boolean
    44. Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal _
    45. lpszExisting As String, ByVal lpszNew As String) As Boolean
    46. Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal _
    47. lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal _
    48. dwFlags As Long, ByRef dwContext As Long) As Boolean
    49. Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile _
    50. As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
    51. Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError _
    52. As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
    53. Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal _
    54. lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
    55. Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData _
    56. As WIN32_FIND_DATA) As Long
    57. Const PassiveConnection As Boolean = True
    58. Private Sub Form_Load()
    59.     'KPD-Team 2000
    60.     'URL: [url]http://www.allapi.net[/url]
    61.     'E-Mail: [email][email protected][/email]
    62.     Dim hConnection As Long, hOpen As Long, sOrgPath  As String
    63.     'open an internet connection
    64.     hOpen = InternetOpen("API-Guide sample program", _
    65.     INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    66.     'connect to the FTP server
    67.     hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT, "your login", "your password", _
    68.     INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
    69.     'create a buffer to store the original directory
    70.     sOrgPath = String(MAX_PATH, 0)
    71.     'get the directory
    72.     FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
    73.     'create a new directory 'testing'
    74.     FtpCreateDirectory hConnection, "testing"
    75.     'set the current directory to 'root/testing'
    76.     FtpSetCurrentDirectory hConnection, "testing"
    77.     'upload the file 'test.htm'
    78.     FtpPutFile hConnection, "C:\test.htm", "test.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0
    79.     'rename 'test.htm' to 'apiguide.htm'
    80.     FtpRenameFile hConnection, "test.htm", "apiguide.htm"
    81.     'enumerate the file list from the current directory ('root/testing')
    82.     EnumFiles hConnection
    83.     'retrieve the file from the FTP server
    84.     FtpGetFile hConnection, "apiguide.htm", "c:\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
    85.     'delete the file from the FTP server
    86.     FtpDeleteFile hConnection, "apiguide.htm"
    87.     'set the current directory back to the root
    88.     FtpSetCurrentDirectory hConnection, sOrgPath
    89.     'remove the direcrtory 'testing'
    90.     FtpRemoveDirectory hConnection, "testing"
    91.     'close the FTP connection
    92.     InternetCloseHandle hConnection
    93.     'close the internet connection
    94.     InternetCloseHandle hOpen
    95. End Sub
    96. Public Sub EnumFiles(hConnection As Long)
    97.     Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
    98.     'set the graphics mode to persistent
    99.     Me.AutoRedraw = True
    100.     'create a buffer
    101.     pData.cFileName = String(MAX_PATH, 0)
    102.     'find the first file
    103.     hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
    104.     'if there's no file, then exit sub
    105.     If hFind = 0 Then Exit Sub
    106.     'show the filename
    107.     Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
    108.     Do
    109.         'create a buffer
    110.         pData.cFileName = String(MAX_PATH, 0)
    111.         'find the next file
    112.         lRet = InternetFindNextFile(hFind, pData)
    113.         'if there's no next file, exit do
    114.         If lRet = 0 Then Exit Do
    115.         'show the filename
    116.         Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
    117.     Loop
    118.     'close the search handle
    119.     InternetCloseHandle hFind
    120. End Sub
    121. Sub ShowError()
    122.     Dim lErr As Long, sErr As String, lenBuf As Long
    123.     'get the required buffer size
    124.     InternetGetLastResponseInfo lErr, sErr, lenBuf
    125.     'create a buffer
    126.     sErr = String(lenBuf, 0)
    127.     'retrieve the last respons info
    128.     InternetGetLastResponseInfo lErr, sErr, lenBuf
    129.     'show the last response info
    130.     MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
    131. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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