INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

File System

Delete a folder and all subfolders and files by zollo9999
Posted: 16 Jun 08 (Edited 24 Jun 08)

Here is a simple method to delete a folder and all files and subfolders. It uses the File system Object. To use it, you will have to set a reference to the 'Microsoft Scripting Runtime' usually found at C:\WINDOWS\system32\scrrun.dll .


CODE

Public Sub DeleteAllFolders(FolderPath As String)

   Dim fso As Scripting.FileSystemObject
   Set fso = New Scripting.FileSystemObject
  
   On Error Resume Next
   fso.DeleteFolder (FolderPath)
   Set fso = Nothing

End Sub

The fso.DeleteFolder method cannot take a trailing "\" in the path so the following code will remove it. [This can be called before inserting the FolderPath parameter or inside the DeleteAllFolders sub before fso.DeleteFolder]

CODE

Function CorrectPath(FolderPath As String) As String
'If FolderPath has trailing backslash, remove it.
    
    FolderPath = Trim(FolderPath)
    If Right(FolderPath, 1) = "\" Then
        CorrectPath = Left(FolderPath, Len(FolderPath) - 1)
    Else
        CorrectPath = FolderPath
    End If
End Function

READ ONLY Files
If any file/document in the folder has the Read-Only attribute set, DeleteFolder will fail with Error 70 Permission Denied. You will have to clear the attribute or delete the file manually.

 

Back to Microsoft: Access Modules (VBA Coding) FAQ Index
Back to Microsoft: Access Modules (VBA Coding) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close