Attribute VB_Name = "modFileAttr"
Option Explicit

Function ChangeFileAttributes(strPath As String, _
                              Optional lngSetAttr As FileAttribute, _
                              Optional lngRemoveAttr As FileAttribute, _
                              Optional blnRecursive As Boolean) As Boolean
    
    ' This function takes a directory path, a value specifying file
    ' attributes to be set, a value specifying file attributes to be
    ' removed, and a flag that indicates whether it should be called
    ' recursively. It returns True unless an error occurs.
    
    Dim fsoSysObj       As FileSystemObject
    Dim fdrFolder       As Folder
    Dim fdrSubFolder    As Folder
    Dim filFile         As File
    
    ' Return new FileSystemObject.
    Set fsoSysObj = New FileSystemObject
    
    On Error Resume Next
    ' Get folder.
    Set fdrFolder = fsoSysObj.GetFolder(strPath)
    If Err <> 0 Then
        ' Incorrect path.
        ChangeFileAttributes = False
        GoTo ChangeFileAttributes_End
    End If
    On Error GoTo 0
    
    ' If caller passed in attribute to set, set for all.
    If lngSetAttr Then
        For Each filFile In fdrFolder.Files
            If Not (filFile.Attributes And lngSetAttr) Then
                filFile.Attributes = filFile.Attributes Or lngSetAttr
            End If
        Next
    End If
    
    ' If caller passed in attribute to remove, remove for all.
    If lngRemoveAttr Then
        For Each filFile In fdrFolder.Files
            If (filFile.Attributes And lngRemoveAttr) Then
                filFile.Attributes = filFile.Attributes - lngRemoveAttr
            End If
        Next
    End If
    
    ' If caller has set blnRecursive argument to True, then call
    ' function recursively.
    If blnRecursive Then
        ' Loop through subfolders.
        For Each fdrSubFolder In fdrFolder.SubFolders
            ' Call function with subfolder path.
            ChangeFileAttributes fdrSubFolder.Path, lngSetAttr, lngRemoveAttr, True
        Next
    End If
    ChangeFileAttributes = True
  
ChangeFileAttributes_End:
    Exit Function
End Function


