SetAttr Statement

See Also2WZXRSQ              Example1COHRPS>Low

Sets attribute information for a file.


SetAttr filename, attributes


The SetAttr statement has the following parts:

Part              Description


filename         Any string expression1330R89 that contains an unambiguous (no wildcards) file specification.  The file specification may include optional drive and path information.

attributes       The sum of any of the following values that characterize file attributes as shown:


Symbolic constant






Normal file



Read-only file



Hidden file



System file



File has changed since last back-up


If you try to set the attributes of a file that is already opened by Visual Basic for anything but read-only access, a run-time error occurs.


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

GetAttr StatementZIM94C

SetAttr Statement Example

The example uses the SetAttr statement to set the Archive attribute of a selected file.  Choosing any file in the list box will cause the Archive attribute to be set for that file if it is not already.  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.  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_ARCHIVE = 32                     ' Declare constants.

   Dim Fname                                   ' Declare variables.

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

      FName = UCase(Dir1.Path & File1.FileName)      ' Get path.


      FName = UCase(Dir1.Path & "\" & File1.FileName)      ' Get path.

   End If

   If GetAttr(FName) And ATTR_ARCHIVE Then     ' Check for Archive.

      MsgBox FName & " already has the Archive attribute."


      SetAttr FName, ATTR_ARCHIVE              ' Set Archive.

      MsgBox FName & " now has the Archive attribute."

   End If

End Sub