GetAttr Function

See Also01NL08X              Example1SODOTJ>Low

Returns an integer that indicates a file, directory, or volume label's attributes.


GetAttr (filename)


The argument filename can be any string expression1330R89 that contains an unambiguous file, directory, or volume label specification.  The file specification may include optional drive and path information.

The value returned by GetAttr is the sum of the following attribute values.


Symbolic constant






Normal file



Read-only file



Hidden file



System file



Volume label



MS-DOS directory



File has changed since last back-up


To determine which attributes are set, use the And operator to perform a bit-wise comparison of the value returned by the GetAttr function and the value of the individual file attribute you want.  If the result is not zero, that attribute is set for the named file.  For example, the Archive attribute is set if the return value of the following And expression is anything but zero, and zero if the Archive attribute is not set:

   Result = GetAttr(FName) And ATTR_ARCHIVE


Note   Symbolic constants for file attribute definitions can be found in the Visual Basic file CONSTANT.TXT.  When placed in any module in a project, the symbolic names can be used in all your form and code modules.


See Also

FileAttr Function2OGE4YB

SetAttr StatementAP4K5W2

GetAttr Function Example

The example uses the GetAttr function to determine the attributes of a file.  Choosing any file in the list box will cause a message box to appear that indicates the files' attributes.  To try this example, paste the code into the Declarations section of a form containing a drive list box, a directory list box, and a file list box, all with default control names.  Set the Hidden and System properties of the file list box to True.  Then press F5 to start.


Sub Drive1_Change ()

   Dir1.Path = Drive1.Drive                 ' Change if drive changes.

End Sub


Sub Dir1_Change ()

   File1.Path = Dir1.Path                   ' Change if directory changes.

End Sub


Sub File1_Click ()

   Const ATTR_READONLY = 1, ATTR_HIDDEN = 2 ' Declare constants.


   Dim Attr, FName, Msg                     ' Declare variables.

   If Right(Dir1.Path,1)="\" Then           ' See if root file.

      FName = Dir1.Path & File1.FileName    ' Get file path.


      FName = Dir1.Path & "\" & File1.FileName ' Get file path.

   End If

   Attr = GetAttr(FName)                    ' Get attributes.

   If Attr > 7 Then Attr = Attr Xor ATTR_ARCHIVE     ' Disregard Archive

   Select Case Attr                         ' Look up attributes.

      Case 0: Msg = "Normal"

      Case ATTR_READONLY: Msg = "Read-Only"

      Case ATTR_HIDDEN: Msg = "Hidden"

      Case ATTR_HIDDEN + ATTR_READONLY: Msg = "Hidden and Read-Only"

      Case ATTR_SYSTEM: Msg = "System"

      Case ATTR_READONLY + ATTR_SYSTEM: Msg = "Read-Only and System"

      Case ATTR_HIDDEN + ATTR_SYSTEM: Msg = "Hidden and System"

      Case ATTR_READONLY + ATTR_HIDDEN + ATTR_SYSTEM: Msg = "Read-Only, Hidden, and System"

   End Select

   MsgBox UCase(FName) & " is a " & Msg & " file."   ' Display message.

End Sub