Gordon_Atherley
asked on
VB6 How to use the FTP functionality of IE6?
G'day Experts
From within my VB6 app I want to harness the FTP functionalities of IE6 for upload and download. Code would be much appreciated, though I'd also be grateful for pointers to third-party controls.
I have one such control in use now, Catalyst's File Transfer Control, which is good, but it doesn't work with IE's functionality.
Thank you
From within my VB6 app I want to harness the FTP functionalities of IE6 for upload and download. Code would be much appreciated, though I'd also be grateful for pointers to third-party controls.
I have one such control in use now, Catalyst's File Transfer Control, which is good, but it doesn't work with IE's functionality.
Thank you
ASKER
Thank you for this code, mladenovicz. I've tested it and it works exactly as it should.
I have a two-part follow-up question, so I've increased the points -- if this is not the preferred way of presenting a follow-up question please let me know, and I'll be pleased to do it differently
I need now to mimic functionality that I use in IE6 when I go to the FTP site by ftp://IP address --
1. to delete an entire folder I've previously placed on the server when the folder has subfolders, each with multiple files
2. to publish the folder/subfolders/files set in fthe first place
From the code above, I'm thinking that I can achieve this by interatively calling the FTPDeleteFile and FTPRemoveDirectory. I'm assuming that there are no permissions problems.
Appreciated
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, the directory does have to be empty before deleting.
Here's where the functonality of IE6 becomes interesting. When I use IE6 to access the directory "Testing" I can delete it even when it's not empty and, indeed, even when it has subdirectories all stuffed with files.
Is this a matter of IE's doing the deleting iteratively as now seems necessary for me, or is there some additional functionality I can use, please?
If anyone has an answer to this, I'd be pleased to enter this as a new question and, therefore, points.
Here's where the functonality of IE6 becomes interesting. When I use IE6 to access the directory "Testing" I can delete it even when it's not empty and, indeed, even when it has subdirectories all stuffed with files.
Is this a matter of IE's doing the deleting iteratively as now seems necessary for me, or is there some additional functionality I can use, please?
If anyone has an answer to this, I'd be pleased to enter this as a new question and, therefore, points.
I found this information on the web:
FtpRemoveDirectory Deletes a directory and all sub items from the FTP site
Maybe it is permisions problem?
Try this:
If Not FtpRemoveDirectory (hConnection, "testing") Then Call ShowError
FtpRemoveDirectory Deletes a directory and all sub items from the FTP site
Maybe it is permisions problem?
Try this:
If Not FtpRemoveDirectory (hConnection, "testing") Then Call ShowError
ASKER
Thanks.
I used thie ShowError code and got the following --
Error 0: 550 Testing The directory is not empty.
I used thie ShowError code and got the following --
Error 0: 550 Testing The directory is not empty.
I am not sure why you can not delete non-empty dir. Try this basModule http://www.thevbzone.com/modFTP.bas
Maybe you should create function that will delete all files and sub dirs. Here is some code I just wrote(not tested)
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Sub DeleteContents(hConnection As Long, DirName As String)
Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
Dim FileOrFolder As String
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the first file
hFind = FtpFindFirstFile(hConnecti on, "*.*", pData, 0, 0)
'if there's no file, then exit sub
If hFind = 0 Then Exit Sub
'set current dir
FtpSetCurrentDirectory hConnection, DirName
FileOrFolder = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
If (pData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
' folder call recursively
Call DeleteContents(hConnection , FileOrFolder)
'set old directory to be current
FtpSetCurrentDirectory hConnection, DirName
Else
FtpDeleteFile hConnection, FileOrFolder
End If
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
FileOrFolder = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
If (pData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
' folder call recursively
Call DeleteContents(hConnection , FileOrFolder)
'set old directory to be current
FtpSetCurrentDirectory hConnection, DirName
Else
FtpDeleteFile hConnection, FileOrFolder
End If
Loop
'close the search handle
InternetCloseHandle hFind
End Sub
So if you want to delete folder testing, your call will look like this:
Call DeleteContents(hConnection , "testing")
FtpSetCurrentDirectory hConnection, "testing"
FtpRemoveDirectory hConnection, "testing"
I am sure that you will have to change this code, but this is just an idea. HTH
Maybe you should create function that will delete all files and sub dirs. Here is some code I just wrote(not tested)
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Sub DeleteContents(hConnection
Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
Dim FileOrFolder As String
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the first file
hFind = FtpFindFirstFile(hConnecti
'if there's no file, then exit sub
If hFind = 0 Then Exit Sub
'set current dir
FtpSetCurrentDirectory hConnection, DirName
FileOrFolder = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
If (pData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
' folder call recursively
Call DeleteContents(hConnection
'set old directory to be current
FtpSetCurrentDirectory hConnection, DirName
Else
FtpDeleteFile hConnection, FileOrFolder
End If
Do
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the next file
lRet = InternetFindNextFile(hFind
'if there's no next file, exit do
If lRet = 0 Then Exit Do
FileOrFolder = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
If (pData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
' folder call recursively
Call DeleteContents(hConnection
'set old directory to be current
FtpSetCurrentDirectory hConnection, DirName
Else
FtpDeleteFile hConnection, FileOrFolder
End If
Loop
'close the search handle
InternetCloseHandle hFind
End Sub
So if you want to delete folder testing, your call will look like this:
Call DeleteContents(hConnection
FtpSetCurrentDirectory hConnection, "testing"
FtpRemoveDirectory hConnection, "testing"
I am sure that you will have to change this code, but this is just an idea. HTH
ASKER
Many thanks, again
I'll keep working, and get back to you.
I'll keep working, and get back to you.
ASKER
I posted a comment earlier but it hasn't surfaced yet, so this note may be out of sequence.
~~~
I used MLADENOVICZ's sub DeleteContents (much appreciated!), see above, for the following test sequence, see below, which funtions as it should.
I am now going to explore iterative deletion through sub directories.
~~~
If anyone would like me to post another question, please signal me with suggestions for the type of question I should ask.
~~~
Public Sub TestSequence()
Dim hConnection As Long, hOpen As Long, sOrgPath As String
'open an internet connection
hOpen = InternetOpen("Tx Sample", INTERNET_OPEN_TYPE_PRECONF IG, vbNullString, vbNullString, 0)
'connect to the FTP server
hConnection = InternetConnect(hOpen, "XX.XX3.XXX.XX", _
INTERNET_DEFAULT_FTP_PORT, _
"username", "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 testing as current directory
FtpSetCurrentDirectory hConnection, "testing"
'upload file
FtpPutFile hConnection, "C:\SunColRailAccidentInve st.pdf", _
"SunColRailAccidentInvest. pdf", _
FTP_TRANSFER_TYPE_UNKNOWN, 0
'delete the file or files
Call DeleteContents(hConnection , "testing")
'set the current directory back to the root
FtpSetCurrentDirectory hConnection, sOrgPath
'remove the directory 'testing'
If Not FtpRemoveDirectory(hConnec tion, "testing") Then Call ShowError
'close the FTP connection
InternetCloseHandle hConnection
'close the internet connection
InternetCloseHandle hOpen
End Sub
~~~
I used MLADENOVICZ's sub DeleteContents (much appreciated!), see above, for the following test sequence, see below, which funtions as it should.
I am now going to explore iterative deletion through sub directories.
~~~
If anyone would like me to post another question, please signal me with suggestions for the type of question I should ask.
~~~
Public Sub TestSequence()
Dim hConnection As Long, hOpen As Long, sOrgPath As String
'open an internet connection
hOpen = InternetOpen("Tx Sample", INTERNET_OPEN_TYPE_PRECONF
'connect to the FTP server
hConnection = InternetConnect(hOpen, "XX.XX3.XXX.XX", _
INTERNET_DEFAULT_FTP_PORT,
"username", "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 testing as current directory
FtpSetCurrentDirectory hConnection, "testing"
'upload file
FtpPutFile hConnection, "C:\SunColRailAccidentInve
"SunColRailAccidentInvest.
FTP_TRANSFER_TYPE_UNKNOWN,
'delete the file or files
Call DeleteContents(hConnection
'set the current directory back to the root
FtpSetCurrentDirectory hConnection, sOrgPath
'remove the directory 'testing'
If Not FtpRemoveDirectory(hConnec
'close the FTP connection
InternetCloseHandle hConnection
'close the internet connection
InternetCloseHandle hOpen
End Sub
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_PRECONF
Const INTERNET_OPEN_TYPE_DIRECT = 1 ' direct to net
Const INTERNET_OPEN_TYPE_PROXY = 3 ' via named proxy
Const INTERNET_OPEN_TYPE_PRECONF
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" (ByRef hInet As Long) As Long
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 InternetGetLastResponseInf
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: KPDTeam@allapi.net
Dim hConnection As Long, hOpen As Long, sOrgPath As String
'open an internet connection
hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONF
'connect to the FTP server
hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT,
'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,
'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,
'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(hConnecti
'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
'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
InternetGetLastResponseInf
'create a buffer
sErr = String(lenBuf, 0)
'retrieve the last respons info
InternetGetLastResponseInf
'show the last response info
MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
End Sub