Put this code in a module
call the function CopyToFTP from your form
The code belong to:
http://www.allapi.net
Good luck
=============================================================
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
Sub CopyToFTP(NomFicDestination, NomFicSource, NomRepDestination, Action)
'KPD-Team 2000
'URL:
http://www.allapi.net
'E-Mail: <email@removed>
Dim PosDiese As Integer
Dim FichierZip As String
Dim FichierEXE As String
Dim NomRepertoireDestination As String 'Nom du repertoire destination
Dim nomRepertoireSource As String
Dim hConnection As Long, hOpen As Long, sOrgPath As String
'open an internet connection
hOpen = InternetOpen("API-Guide sample program",
INTERNET_OPEN_TYPE_PROXY, "enter a IP if you have proxy with the port ex:
192.168.123.202:21", vbNullString, 0)
'connect to the FTP server
hConnection = InternetConnect(hOpen, "Your DOMAIN", _
INTERNET_DEFAULT_FTP_PORT, "USer NAME", "PASSWORD", _
INTERNET_SERVICE_FTP, IIf(PassiveConnection,
INTERNET_FLAG_PASSIVE, 0), 0)
'PosDiese = InStr(1, NomFic, "#")
'FichierZip = Mid(NomFic, 1, PosDiese)
'FichierEXE = Mid(NomFic, PosDiese + 1)
'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, NomRepDestination
'set the current directory to 'root/testing'
FtpSetCurrentDirectory hConnection, NomRepDestination
If Action = True Then
'upload the file 'test.htm'
FtpPutFile hConnection, NomFicSource, NomFicDestination,
FTP_TRANSFER_TYPE_BINARY, 0
Else
FtpGetFile hConnection, NomFicSource, NomFicDestination, False,
0, FTP_TRANSFER_TYPE_BINARY, 0
End If
'rename 'test.htm' to 'apiguide.htm'
'FtpRenameFile hConnection, "test.txt", "apiguide.txt"
'enumerate the file list from the current directory ('root/testing')
'EnumFiles hConnection
'delete the file from the FTP server
'FtpDeleteFile hConnection, "apiguide.txt"
'set the current directory back to the root
'FtpSetCurrentDirectory hConnection, sOrgPath
'remove the direcrtory 'testing'
'FtpRemoveDirectory hConnection, "testing"
ShowError
'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
'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
Debug.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
Debug.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