delete window directory macro code example

Example: delete window directory macro

'VBA Deleting All Files and Subfolders
Sub VBAF1_Delete_All_Files_and_Subfolders()
    
    'Variable declaration
    Dim sFolderPath As String
    Dim oFSO As FileSystemObject
    
     'Define Folder Path
    sFolderPath = "D:\Attachment\"
    
    'Check if slash is added
    If Right(sFolderPath, 1) = "\" Then
        'If added remove it from the specified path
        sFolderPath = Left(sFolderPath, Len(sFolderPath) - 1)
    End If
            
    'Create FSO Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    
    'Check Specified Folder exists or not
    If oFSO.FolderExists(sFolderPath) Then
    
          'Delete All Files
          oFSO.DeleteFile sFolderPath & "\*.*", True
                        
          'Delete All Subfolders
          oFSO.DeleteFolder sFolderPath & "\*.*", True
          
     End If
    
End Sub

Tags:

Misc Example