Function to delete a folder and its content

J

John J.

I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John
 
A

Arvin Meyer [MVP]

I would simple delete it and handle any errors (untested):

Sub KillDir(strPath As String)
On Error Resume Next
Kill strDir
End Sub
 
J

John J.

Thanks, but it doesn't work. Deleting a folder with the kill statement
doesn't work (error: file cannot be found). The Help file says I should use
rmdir.
John
 
H

H. Druss

John J. said:
I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John
Hi John
Try this. Needs some error handling probably.
====================================================================================
Option Explicit
Private Declare Function RemoveDirectory Lib "kernel32" Alias
"RemoveDirectoryA" (ByVal lpPathName As String) As Long

Private Sub Command1_Click()
Dim sPath As String
sPath = "c:\DeleteThisFolder"
RemoveFilesAndFolder sPath
End Sub

Sub RemoveFilesAndFolder(sPath As String)
Dim ff As String
' need the trailing back slash
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
ff = Dir(sPath)
If ff <> "" Then
Kill sPath & "*.*"
End If
RemoveDirectory sPath
End Sub
===========================================================================================
Good luck
Harold
 
J

John J.

Thanks. But with this code only files in the main folder gets deleted.
The main folder itself, subfolders and subfolder files aren't deleted.
John
 
H

H. Druss

John J. said:
I noticed that rmdir doesnt'work if there are subfolders and files in the
folder. Is there some function available that first clears all contents of
the folder and then deletes the folder itself?
Thank you,
John
Hi John
Try this:
===================================================================
Private Sub RemoveFoldersAndFiles(sPath As String)
If Right(sPath, 1) = "\" Then sPath = Left$(sPath, Len(sPath) - 1)
Dim fso As New Scripting.FileSystemObject
fso.DeleteFolder sPath

'You can also pass a second argument and set it to True if you want to force
the deletion of read-only files:
'fso.DeleteFolder sPath, True

End Sub
=======================================================================
Private Sub Command1_Click()

RemoveFoldersAndFiles "c:\theFolder"

End Sub
========================================================================

You need to add a reference to the Microsoft Scripting Runtime.

Good luck
Harold
 
J

John J.

Yes! it works.
Thanks so much.
John

H. Druss said:
Hi John
Try this:
===================================================================
Private Sub RemoveFoldersAndFiles(sPath As String)
If Right(sPath, 1) = "\" Then sPath = Left$(sPath, Len(sPath) - 1)
Dim fso As New Scripting.FileSystemObject
fso.DeleteFolder sPath

'You can also pass a second argument and set it to True if you want to
force the deletion of read-only files:
'fso.DeleteFolder sPath, True

End Sub
=======================================================================
Private Sub Command1_Click()

RemoveFoldersAndFiles "c:\theFolder"

End Sub
========================================================================

You need to add a reference to the Microsoft Scripting Runtime.

Good luck
Harold
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top