The VBA GetAttr Function

Related Functions:
VBA FileDateTime
VBA FileLen

Description

The VBA GetAttr function returns an integer, representing the attributes of a supplied file, directory or folder.

The syntax of the function is:

GetAttr( PathName )

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:

vbFileAttributeValueAttribute
vbNormal0Normal
vbReadOnly1Read Only
vbHidden2Hidden
vbSystem4System File (not available on the Macintosh)
vbDirectory16Directory or Folder
vbArchive32File has changed since the last backup (not available on the Macintosh)
vbAlias64Supplied 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:

GetAttr( PathName ) And vbReadOnly

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.


VBA GetAttr Function Examples

Example 1 - Single Attribute Returned

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.
Dim rdOnly As Integer
rdOnly = attr And vbReadOnly
' rdOnly is now equal to 1 (file is read only).
' Test if file is hidden, using the bitwise And Operator.
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:


Example 2 - Multiple Attributes Returned

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.
Dim rdOnly As Integer
rdOnly = attr And vbReadOnly
' rdOnly is now equal to 1 (directory is read only).
' Test if hidden, using the bitwise And Operator.
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:



VBA GetAttr Function Error

If the PathName that is supplied to the VBA GetAttr function does not relate to an existing file, you will get the error:

Run-time error '76': Path not found

VBA Run Time Error 76 Message Box