FTP upload and download with VB

Source: Internet
Author: User
Tags filetime ftp integer
Upload | download Option Explicit

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 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

Public Declare Function ftpgetfile Lib "Wininet.dll" Alias "Ftpgetfilea" _
(ByVal hftpsession as Long, ByVal Lpszremotefile as String, _
ByVal Lpsznewfile as String, ByVal ffailifexists as Boolean, _
ByVal dwFlagsAndAttributes as Long, ByVal dwflags as Long, _
ByVal Dwcontext as Long) as Boolean

Public Declare Function ftpputfile Lib "Wininet.dll" Alias "Ftpputfilea" _
(ByVal hftpsession as Long, ByVal Lpszlocalfile as String, _
ByVal Lpszremotefile as String, ByVal dwflags as Long, _
ByVal Dwcontext as Long) as Boolean

Public Declare Function ftpdeletefile Lib "Wininet.dll" Alias "Ftpdeletefilea" _
(ByVal hftpsession as Long, ByVal lpszFileName as String) As Boolean

Public Declare Function ftprenamefile Lib "Wininet.dll" Alias "Ftprenamefilea" _
(ByVal hftpsession as Long, ByVal lpszexsiting As String, ByVal lpsznew as String) As Boolean

Public Declare Function internetclosehandle Lib "Wininet.dll" (ByVal hInet as Long) as Integer

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, lpvfnddata as Win32_find_data) As Long

Public Type FILETIME
Dwlowdatetime as Long
Dwhighdatetime as Long
End Type
Public Type Win32_find_data
Dwfilattributes 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 * 260
Calternate as String * 14
End Type


Public Declare Function getopenfilename Lib "Comdlg32.dll" Alias _
"Getopenfilenamea" (Popenfilename as OpenFileName) as Long

Type OpenFileName
lStructSize as Long
hWndOwner as Long
HInstance as Long
Lpstrfilter as String
Lpstrcustomfilter as String
Nmaxcustfilter as Long
Nfilterindex as Long
Lpstrfile as String
nMaxFile as Long
Lpstrfiletitle as String
Nmaxfiletitle as Long
lpstrInitialDir as String
Lpstrtitle as String
Flags as Long
Nfileoffset as Integer
Nfileextension as Integer
Lpstrdefext as String
Lcustdata as Long
Lpfnhook as Long
Lptemplatename as String
End Type
Private Sub Command1_Click ()
Lnginet = InternetOpen (vbNullString, Internet_open_type_preconfig, _
vbNullString, vbNullString, 0&)
If lnginet Then
Lnginetconn = InternetConnect (lnginet, "IP address", 0, _
"username", "password", 1, 0, 0)
If Lnginetconn Then
BLNRC = FtpGetFile (Lnginetconn, "website/address.asp", "c:\address.asp", 0, 0, 1, 0)
If BLNRC Then
MsgBox "Download OK!!!"
End If
InternetCloseHandle Lnginetconn
InternetCloseHandle lnginet
MsgBox "Close OK!!!"
Else
MsgBox "can ' t connect"
End If
Else
MsgBox "ftp Wrong"
End If
End Sub

Private Sub Command2_Click ()
Dim PData as Win32_find_data
Dim Lnghinet as Long
Dim Interror as Integer
Dim strtemp as String
Dim BLNRC as Boolean
Pdata.cfilename = String (260, 0)
Lnginet = InternetOpen (vbNullString, Internet_open_type_preconfig, _
vbNullString, vbNullString, 0&)
If lnginet Then
Lnginetconn = InternetConnect (lnginet, "IP address", 0, _
"username", "password", 1, 0, 0)
If Lnginetconn Then
Lnghinet = Ftpfindfirstfile (Lnginetconn, "*.*", PData, 0, 0)
strtemp = Left (Pdata.cfilename, InStr (1, Pdata.cfilename, String (1, 0), Vbbinarycompare)-1)
Todo
Pdata.cfilename = String (260, 0)
BLNRC = Internetfindnextfile (lnghinet, PData)
If not BLNRC Then
Exit do
Else
strtemp = Left (Pdata.cfilename, InStr (1, Pdata.cfilename, String (1, 0), Vbbinarycompare)-1)
End If

Loop
Text1.Text = strtemp
End If
End If
InternetCloseHandle lnghinet

InternetCloseHandle Lnginetconn
InternetCloseHandle lnginet


End Sub





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.