Just FYI... I had to write something like that for an app of mine. If the user wants to empty the root upload folder (i.e. get rid of old files) before re-uploading new ones, I had to figure out a way to recursively delete all files/folders in the root. Here's what I came up with:
I just pass ClearFTP the FTPClient object, and the root folder that I want to empty... it will loop through all subfolders and empty them, then delete the subfolder. (if you want to empty files that are in the root folder as well, just do the same type of DirDetails loop before calling ClearFTP) It then passes back a string containing any errors that occured while it was trying to delete.
Private Shared Function ClearFTP(ByRef ftp As FTPClient, ByVal sFldr As String) As String
Dim fl, fld() As FTPFile, sErr As String, i As Integer
Try
fld = ftp.DirDetails(sFldr)
For i = 0 To UBound(fld)
If fld(i).Dir And (Not fld(i).Name = "." And Not fld(i).Name = "..") Then
For Each fl In ftp.DirDetails(sFldr & "/" & fld(i).Name)
Try
If Not fl.Dir Then ftp.Delete(sFldr & "/" & fld(i).Name & "/" & fl.Name)
Catch ex As Exception
sErr &= "Could not delete: " & sFldr & "/" & fld(i).Name & "/" & fl.Name & ". Details: " & ex.Message & vbCrLf
End Try
Next
sErr &= ClearFTP(ftp, sFldr & "/" & fld(i).Name)
Try
ftp.RmDir(sFldr & "/" & fld(i).Name)
Catch
End Try
End If
Next
fld = Nothing
ClearFTP = sErr
Catch ex As Exception
ClearFTP = sErr & "An error occured while trying to delete from " & sFldr & ". Details: " & ex.Message & vbCrLf
End Try
End Function
This works great except for a bug in DirDetails which doesn't always list all the files in a folder (not sure why). I would use Dir (which correctly lists the files every time), but then I can't differentiate between what's a folder and what's a file.
WATYF