Returning and Setting File Attributes the Get Attr and Set Attr Functions

Files and folders can have characteristics such as "read-only," "hidden," "system," and "archive."

These characteristics are called attributes. To find out the attributes of a file or folder, use the GetAttr function. The only argument of this function is the name of the file or folder that you want to work with:


The above function returns an integer that represents the sum of one or more of the constants shown below: Table 8-2: File and folder attributes






Normal (other attributes are not set)



Read-only (file or folder can't be modified)



Hidden (file or folder isn't visible under normal setup)



System file



The object is a directory



Archive (the file has been modified since it was last

backed up)

backed up)

To find out whether a file has any of the attributes shown above, use the AND operator to compare the result of the GetAttr function with the value of the constant. If the function returns a non-zero value, the file or folder specified in the pathname has the attribute for which you are testing.

What are the attributes of C:\MsDos.sys? You can find out quickly in the Immediate window:

?getattr("C:\MsDos.sys") AND vbReadOnly 1

?getattr("C:\MsDos.sys") AND vbHidden 2

?getattr("C:\MsDos.sys") AND vbSystem 4

?getattr("C:\MsDos.sys") AND vbArchive 32

Now let's put this information together in a procedure:

1. Insert a new module and rename it GetAttrFunction.

2. Enter the following GetAttributes procedure:

Sub GetAttributes() Dim attr As Integer Dim msg As String attr = GetAttr("C:\MSDOS.SYS")

If attr AND vbReadOnly Then msg = msg & "Read-Only (R)" If attr AND vbHidden Then msg = msg & Chr(10) & "Hidden (H)" If attr AND vbSystem Then msg = msg & Chr(10) & "System (s)" If attr AND vbArchive Then msg = msg & Chr(10) & "Archive (A)" MsgBox msg, , "MSDOS.SYS" End Sub

3. When you run the above procedure, you should see the message box shown in Figure 8-1.

Figure 8-1:

You can get the attributes of any file using the GetAttr function.

The opposite of the GetAttr function is the SetAttr function, which allows you to set the attributes for a file or a folder. Its syntax is:

SetAttr Pathname, Attributes

Pathname is a string that specifies the file or folder that you want to work with. The second argument, Attributes, is one or more constants that specify the attributes you want to set. See Table 8-1 earlier in this section for the list of constants.

Suppose you have a file called "C:\stamps.txt" and you want to set two attributes, "read-only" and "hidden." To set the file attributes, enter the

following instruction in the Immediate window (replace the "C:\stamps.txt" with the name of a file that exists on your disk):

SetAttr "C:\stamps.txt", vbReadOnly + vbHidden

Tip 8-2: Invoking the SetAttr Statement

You cannot set the attributes of an open file. You must close the file before using the SetAttr function.

Was this article helpful?

+2 -1


Post a comment