The VBA GetAttr function returns an integer, representing the attributes of a supplied file, directory or folder.
The syntax of the function is:
Where the PathName argument provides the path of the file, folder or directory that you want details of.
The function returns an integer, which is the sum of all the vbFileAttribute enumerator values that apply to the supplied path.
Possible values that may make up the returned integer are:
vbFileAttribute | Value | Attribute |
---|---|---|
vbNormal | 0 | Normal |
vbReadOnly | 1 | Read Only |
vbHidden | 2 | Hidden |
vbSystem | 4 | System File (not available on the Macintosh) |
vbDirectory | 16 | Directory or Folder |
vbArchive | 32 | File has changed since the last backup (not available on the Macintosh) |
vbAlias | 64 | Supplied filename is an alias (only available on the Macintosh) |
Testing for an Attribute
It is suggested that you test for an attribute of a file using the bitwise And operator. For example, to test if a file is read only, you could use the expression:
If the value returned from the above expression is zero, this indicates that the file is not read only, but if the expression evaluates to a non-zero value, this indicates that the file is read only. Examples of this are provided below.
The following example uses the VBA GetAttr function to return the attributes of a read-only text file.
' Get the attributes of the file data.txt.
Dim attr As Integer attr = GetAttr( "C:\Users\John\Documents\data.txt" ) ' attr is now equal to 1 (indicates a read-only file).
' Test if file is read only, using the bitwise And Operator.
' Test if file is hidden, using the bitwise And Operator.Dim rdOnly As Integer rdOnly = attr And vbReadOnly ' rdOnly is now equal to 1 (file is read only). Dim hdn As Integer hdn = attr And vbHidden ' hdn is now equal to 0 (file is not hidden). |
After running the above VBA code, the variable attr is equal to 1, which represents the enumerator value vbReadOnly.
The above code also uses the bitwise And operator, to test if the file is read only, and to test if it is hidden. This returns:
The following example uses the VBA GetAttr function to return the attributes of a read-only directory.
' Get the attributes of the directory C:\Users.
Dim attr As Integer attr = GetAttr( "C:\Users" ) ' attr is now equal to 17 (indicates a read-only directory).
' Test if a directory, using the bitwise And Operator.
Dim isdirectory As Integer isdirectory = attr And vbDirectory ' isdirectory is now equal to 16 (the path refers to a directory).
' Test if read only, using the bitwise And Operator.
' Test if hidden, using the bitwise And Operator.Dim rdOnly As Integer rdOnly = attr And vbReadOnly ' rdOnly is now equal to 1 (directory is read only). Dim hdn As Integer hdn = attr And vbHidden ' hdn is now equal to 0 (directory is not hidden). |
After running the above VBA code, the variable attr is equal to 17, which represents the enumerator values vbReadOnly + vbDirectory (=1+16).
The above code also uses the bitwise And operator, to test if the supplied path is a directory, is read only or is hidden. This returns:
If the PathName that is supplied to the VBA GetAttr function does not relate to an existing file, you will get the error: