Si seleccionas eliminar las subcarpetas (que es lo normal) se borrarán todos los archivos y subcarpetas de la misma, y por ultimo se borra tambien la carpeta.
Si no seleccionas borrar las subcarpetas solamente se borran los archivos de la carpeta elegida y posteriormente se intenta borrar la carpeta, pero si existen subcarpetas está nunca se conseguirá eliminar (ya que hay subcarpetas dentro) y la funcion devolverá un valor distinto de 0.
Weno hice dos funciones, una usando las APIs y otra usando las funciones de VB:
Código
Option Explicit Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long Private Declare Function RemoveDirectory Lib "kernel32" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long Public Function ElimFolder(ByVal sDir As String, ByVal WithSubfolders As Boolean) As Long Dim sFile As String Dim counD As Long, counF As Long Dim i As Long ReDim sDirs(0 To 0) As String ReDim sFiles(0 To 0) As String On Error Resume Next If IsDir(sDir) Then If Right(sDir, 1) <> "\" Then sDir = sDir & "\" sFile = Dir(sDir, 55) Do If IsDir(sDir & sFile) Then If (sFile <> ".") And (sFile <> "..") Then ReDim Preserve sDirs(0 To counD) As String sDirs(counD) = sDir & sFile counD = counD + 1 End If Else ReDim Preserve sFiles(0 To counF) As String sFiles(counF) = sFile counF = counF + 1 End If sFile = Dir Loop While sFile <> vbNullString If sFiles(0) <> vbNullString Then For i = 0 To UBound(sFiles) DeleteFile sDir & sFiles(i) Next i End If If WithSubfolders Then If sDirs(0) <> vbNullString Then For i = 0 To UBound(sDirs) ElimFolder sDirs(i), True Next i End If End If ElimFolder = RemoveDirectory(sDir) End If End Function
Código
Option Explicit Public Function ElimFolder(ByVal sDir As String, ByVal WithSubfolders As Boolean) As Long Dim sFile As String Dim counD As Long, counF As Long Dim i As Long ReDim sDirs(0 To 0) As String ReDim sFiles(0 To 0) As String On Error Resume Next If IsDir(sDir) Then If Right(sDir, 1) <> "\" Then sDir = sDir & "\" sFile = Dir(sDir, 55) Do If IsDir(sDir & sFile) Then If (sFile <> ".") And (sFile <> "..") Then ReDim Preserve sDirs(0 To counD) As String sDirs(counD) = sDir & sFile counD = counD + 1 End If Else ReDim Preserve sFiles(0 To counF) As String sFiles(counF) = sFile counF = counF + 1 End If sFile = Dir Loop While sFile <> vbNullString If sFiles(0) <> vbNullString Then For i = 0 To UBound(sFiles) Kill sDir & sFiles(i) Next i End If If WithSubfolders Then If sDirs(0) <> vbNullString Then For i = 0 To UBound(sDirs) ElimFolder sDirs(i), True Next i End If End If Err.Clear RmDir sDir If Err.Number = 0 Then ElimFolder = 1 End If End Function
Son codes sencillos pero espero que les sirvan a alguien.
Saludos