VB and VBA Users Source Code: Removing/deleting a directory and all it's subdirectories
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Removing/deleting a directory and all it's subdirectories
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, July 05, 2005
Hits:
1649
Category:
Files/Directories/IO
Article:
The code below is an enhanced version of RmDir. It will delete a folder and files and sub folders contained within the specified folder. 'Purpose : Deletes a directory and all the sub directories below it. 'Inputs : sFolder The directory/folder to remove. ' [bDeleteReadonly] If true will delete readonly files/directories 'Outputs : Returns true on success, else returns false. 'Date : 25/03/2001 'Notes : Function DeleteDirectory(ByVal sFolder As String, Optional bDeleteReadonly As Boolean = False) As Boolean Dim sThisFile As String Dim bCheckingFolders As Boolean Dim sFailedFolderDeletes As String sFolder = Trim$(sFolder) If Len(sFolder) = 0 Then 'Folder doesn't exist DeleteDirectory = False End If 'Append trailing slash If Right$(sFolder, 1) <> "\" Then sFolder = sFolder & "\" End If 'Ignore any errors On Error Resume Next If GetAttr(sFolder) And vbDirectory = 0 Then 'Folder doesn't exist DeleteDirectory = False Exit Function End If On Error GoTo ErrFailed 'Delete files sThisFile = Dir$(sFolder) Do 'Check if something was returned If Len(sThisFile) = 0 Then 'Nothing returned If bCheckingFolders = False Then 'No more files, get the directories bCheckingFolders = True sThisFile = Dir$(sFolder, vbDirectory) Else 'No more folders or files Exit Do End If End If If sThisFile <> "." And sThisFile <> ".." Then If GetAttr(sFolder & sThisFile) And vbDirectory Then 'Check we haven't already tried to delete this folder If InStr(1, sFailedFolderDeletes, sFolder & sThisFile & "|") = 0 Then 'Delete a directory If DeleteDirectory(sFolder & sThisFile, bDeleteReadonly) = False Then 'Failed, add the folder to list of failed folders (to prevent getting stuck in loop) sFailedFolderDeletes = sFailedFolderDeletes & sFolder & sThisFile & "|" End If End If 'Restart the folder scanning (otherwise the dir function just returns the same folder) sThisFile = Dir$(sFolder, vbDirectory) Else 'Found a file If bDeleteReadonly Then 'Delete readonly files If GetAttr(sFolder & sThisFile) And vbReadOnly Then Call SetAttr(sFolder & sThisFile, vbNormal) End If End If Kill sFolder & sThisFile 'Get the next file sThisFile = Dir$ End If Else 'Get the next folder/file sThisFile = Dir$ End If Loop If bDeleteReadonly Then 'Delete readonly folders If GetAttr(sFolder) And vbReadOnly Then Call SetAttr(sFolder, vbNormal) End If End If 'Try and remove the folder RmDir sFolder 'Success DeleteDirectory = True Exit Function ErrFailed: Debug.Print "Error in DeleteDirectory while deleting '" & sFolder & "'. Error : " & Err.Description 'Failed to remove the directory DeleteDirectory = False End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder