O objeto File e o objeto Folder posseum propriedades de atributo que podem ser usadas para ler ou definir seus respectivos atributos.
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 fdrSubFolder As Folder
' Return new FileSystemObject.
Set fsoSysObj = New FileSystemObject
Set fdrFolder = fsoSysObj.GetFolder(strPath)
Let ChangeFileAttributes = False
GoTo ChangeFileAttributes_End
' If caller passed in attribute to set, set for all.
For Each filFile In fdrFolder.Files
If Not (filFile.Attributes And lngSetAttr) Then
Let filFile.Attributes = filFile.Attributes Or lngSetAttr
' If caller passed in attribute to remove, remove for all.
For Each filFile In fdrFolder.Files
If (filFile.Attributes And lngRemoveAttr) Then
Let filFile.Attributes = filFile.Attributes - lngRemoveAttr
' If caller has set blnRecursive argument to True, then call
' Loop through subfolders.
For Each fdrSubFolder In fdrFolder.SubFolders
' Call function with subfolder path.
ChangeFileAttributes fdrSubFolder.Path, lngSetAttr, lngRemoveAttr, True
Let ChangeFileAttributes = True
ChangeFileAttributes_End:
A função ChangeFileAttributes leva quatro argumentos: o caminho para uma pasta, uma constante opcional que especifica os atributos para definir, uma constante opcional que especifica os atributos de remover, e um argumento opcional que especifica se a função deve ser chamado recursivamente.
Se o caminho da pasta passado for válido, o procedimento retorna um objeto Folder. Em seguida, ele verifica se o argumento lngSetAttr foi fornecido. Se assim for, ele percorre todos os arquivos na pasta, acrescentando-lhes o novo atributo a todos os arquivos existentes. Utiliza o argumento lngRemoveAttr, removendo os atributos especificados se eles existirem para os arquivos da coleção.
Finalmente, o procedimento verifica se o argumento blnRecursive foi definido como True. Se assim for, chama o procedimento para cada arquivo em cada subpasta do argumento strPath.
Tags: VBA, Tips, files, directory, folder, Scripting Runtime object library, Scrrun.dll, FileSystemObject, GetFiles, ChangeFileAttributes,