Delete file from ftp server

Ghostman

New member
Local time
Today, 06:40
Joined
Mar 18, 2014
Messages
3
Hi, I need a little help with a deleting a file from a ftp server. I have the code for uploading but I can't seem to figure out how to delete a file.

this is the FTP module I'm using
Code:
 Public Sub UploadFileToFTPServer()
On Error GoTo ErrHandler
Dim lngRet As Long
Dim abytData() As Long
Dim lngBytesWritten As Long
Dim lngBytesRead As Long
Dim lngTotalBytesWritten As Long
Const conERR_COULD_NOT_TRANSFER_FILE = vbObjectError + 2215
   Call SysCmd(acSysCmdSetStatus, "A mudar de pasta....")
   Call sChangeDir(mstrDestination)
   Call SysCmd(acSysCmdSetStatus, "A carregar o ficheiro. Por favor aguarde...")
  
  lngRet = apiFTPPutFile(hSession, mstrSrcFile, mstrDestination, _
     INTERNET_FLAG_TRANSFER_BINARY Or INTERNET_FLAG_NO_CACHE_WRITE, 0&)
    
  If lngRet = 0 Then Err.Raise conERR_COULD_NOT_TRANSFER_FILE
  
  Call SysCmd(acSysCmdSetStatus, "Pronto...")
 ExitHere:
  On Error Resume Next
  Call SysCmd(acSysCmdClearStatus)
  Exit Sub
ErrHandler:
  Select Case Err.Number
    Case conERR_COULD_NOT_TRANSFER_FILE:
      Err.Raise conERR_COULD_NOT_TRANSFER_FILE, "FTP::UploadFileToFTPServer", _
        fInetError(Err.LastDllError)
    Case Else
      With Err
        .Raise .Number, .Source, fInetError(.LastDllError)
      End With
  End Select
  Resume ExitHere
End Sub
and this is the code for uploading i use in the form when i click a button

Code:
Dim objFTP As FTP
        Const conTARGET = "ftpserver"
        Set objFTP = New FTP
        With objFTP
            .FtpURL = conTARGET
            .SourceFile = "sourcefile"
             .DestinationFile = "destination file"
            .AutoCreateRemoteDir = True
            If Not .IsConnected Then .DialDefaultNumber
            .ConnectToFTPHost "username", "password"
            .UploadFileToFTPServer
            
        End With
all that works like a charm.

Could someone help me with a code to delete a file from the ftp server, i'm kind of stuck

thanks in advance
 
Last edited:
Thanks spikepl, i'll take a look at that :)
 
Problem solved :D

I found a code that I altered to my needs and it works great for uploading and for deleting.

here it is in case anyone else needs it too.

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

[COLOR=red]Private Sub button_click()
Dim hConnection As Long, hOpen As Long, sOrgPath As String
[/COLOR]
 [COLOR=red]'opens an internet connection
hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, _ vbNullString, vbNullString, 0)
[/COLOR]
 [COLOR=red]'connects 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)
[/COLOR]
 [COLOR=red]'creates a buffer to store the original directory
sOrgPath = String(MAX_PATH, 0)
[/COLOR]
 [COLOR=red]'gets the directory
FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
[/COLOR]
 [COLOR=red]'creates a new directory called 'testing' to root (in some servers you might need to add public_html/  or  www/  before the name of the folder)
FtpCreateDirectory hConnection, "testing"
[/COLOR]
 [COLOR=red]'sets the current directory to 'root/testing'
FtpSetCurrentDirectory hConnection, "testing"
[/COLOR]
 [COLOR=red]'uploads the file 'test.htm'
FtpPutFile hConnection, "C:\\test.htm", "test.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0
[/COLOR]
 [COLOR=red]'renames 'test.htm' to 'apiguide.htm'
FtpRenameFile hConnection, "test.htm", "apiguide.htm"
[/COLOR]
 [COLOR=red]'enumerates the file list from the current directory ('root/testing')
EnumFiles hConnection
[/COLOR]
 [COLOR=red]'downloads the file from the FTP server
FtpGetFile hConnection, "apiguide.htm", "c:\\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
[/COLOR]
 [COLOR=red]'deletes the file from the FTP server
FtpDeleteFile hConnection, "apiguide.htm"
[/COLOR]
 [COLOR=red]'sets the current directory back to the root
FtpSetCurrentDirectory hConnection, sOrgPath
[/COLOR]
 [COLOR=red]'removes the directory 'testing'
FtpRemoveDirectory hConnection, "testing"
[/COLOR]
 [COLOR=red]'closes the FTP connection
InternetCloseHandle hConnection
[/COLOR]
 [COLOR=red]'closes the internet connection
InternetCloseHandle hOpen
[/COLOR]
 [COLOR=red]End Sub
[/COLOR]
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

the bit in red is the part you need to alter to your needs.

Once again thans for the help spikepl.
 

Users who are viewing this thread

Back
Top Bottom