Managing Folder Attributes

Microsoft® Windows® 2000 Scripting Guide

File systems typically support the concept of attributes, information about a file or folder that goes beyond the folder name and size. For example, if you right-click a folder in Windows Explorer and then click Properties, you can access the attributes for that folder. Figure 4.2 shows a folder with the following attributes set:

  • Read-only

  • Hidden

  • Ready for archiving

  • Compressed

Figure 4.2 Folder Attributes in Windows Explorer

sas_scr_02c

The FileSystemObject can be used to return several important attributes of a folder. These attributes, and their FileSystemObject values, are listed in Table 4.4.

Table 4.4 Folder Attributes Used by the FileSystemObject

Constant

Value

Description

Hidden

2

Indicates that the folder is hidden, and not visible by default in My Computer or Windows Explorer.

System

4

Indicates that the folder is a System folder. In general, it is a good idea not to modify the properties of a system folder.

Directory

16

Standard value applied to all folders. All folders accessed by the FileSystemObject will have, at a minimum, the bit value 16.

Archive

32

Archive bit used by backup programs to determine the files and folders that need to be backed up. Enabling the archive bit will ensure that the folder is backed up during the next incremental backup. Disabling the archive bit will prevent the folder from being backed up during the next incremental backup.

Compressed

2048

Indicates whether Windows compression has been used on the folder.

The values listed in Table 4.4 are the only values that can be retrieved or configured by using the FileSystemObject. Although this seems simple enough, the data returned to you by the FileSystemObject can be confusing at first. For example, if you echo the value of the Attributes property for a folder, you might see a value like 20, a value that does not appear in the list of valid attribute values.

In addition, you will receive only a single value, even if a folder has all possible attributes (that is, it is a hidden, compressed system folder ready for archiving). In a case such as this, your script will not display the values 2, 4, 16, 32, and 2048 but instead will display the value 2102. This is because attribute values are always returned in the form of a bitmap.

Note

  • With attributes, the term bitmap refers to the way data is stored and returned. It should not be confused with bitmap images, such as .BMP files.

Working with Bitmaps

A bitmap is like a set of switches that can be either on or off. If a particular switch is off, that switch has the value 0. If the switch is on, at least in the case of a folder object, it has one of the values shown in Table 4.4. The value of the bitmap is equal to the sum of all the switches.

For example, a highly simplified illustration of a folder object bitmap is shown in Figure 4.3. In this example, only one individual switch, Directory, is on. Directory has the value 16. Because the other switches are off, each has the value 0. The total value for the bitmap is thus 16. If you queried the Attributes value for this folder, the script would return 16.

Figure 4.3 First Sample Bitmap Representation

sas_scr_03s

By comparison, the folder object shown in Figure 4.4 has three switches activated: Hidden (with the value 2), Directory (with the value 16), and Compressed (with the value 2048). The value for this bitmap would thus be 2 + 16 + 2048, or 2066. This is also the value that would be returned by a script querying this folder for its Attributes value.

Figure 4.4 Second Sample Bitmap Representation

sas_scr_04s

Bitmaps are designed so that there is only one possible way to achieve a given value. The only way for a folder attribute to return the value 2066 is for it to be a hidden and compressed folder. It is mathematically impossible to return a 2066 with any other combination of attributes.

This design enables you to take the return value and determine which switches have been set and which ones have not; in turn, this allows you to determine the attributes of the folder. If you receive the return value 2066, you know that the only way to receive that value is to have a hidden and compressed folder.

Fortunately, you do not have to perform any sort of mathematical calculations to derive the individual attributes. Instead, you can use the logical AND operator to determine whether an individual switch is on or off. For example, the following code sample checks to see whether the folder is hidden; if it is, the script echoes the message "Hidden folder."

If objFolder.Attributes AND 2 Then
    Wscript.Echo "Hidden folder."
End If

Although the If Then statement might appear a bit strange, it makes a little more sense when read like this: "If the attributes switch with the value 2 is on, then ..." Likewise, this statement would read, "If the attributes switch with the value 16 is on, then ..."

If objFolder.Attributes AND 16 Then

The script in Listing 4.13 binds to the folder C:\FSO and then echoes the folder attributes.

Listing 4.13 Enumerating Folder Attributes

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\FSO")
If objFolder.Attributes AND 2 Then
 Wscript.Echo "Hidden folder."
End If
If objFolder.Attributes AND 4 Then
 Wscript.Echo "System folder."
End If
If objFolder.Attributes AND 16 Then
 Wscript.Echo "Folder."
End If
If objFolder.Attributes AND 32 Then
 Wscript.Echo "Archive bit set."
End If
If objFolder.Attributes AND 2048 Then
 Wscript.Echo "Compressed folder."
End If