Changing Folder Attributes

Microsoft® Windows® 2000 Scripting Guide

As explained in "Working with Bitmaps," individual folder attributes can be likened to switches. If the switch for Hidden is on, the folder is a hidden folder. If the switch for Hidden is off, the folder is not a hidden folder.

This analogy can be carried further by noting that light switches are typically under your control: you can choose to turn them on, or you can choose to turn them off. The same thing is true of folder attributes: as with other switches, you can turn these attribute switches on, or you can turn them off.

You can use scripts to toggle these switches on or off (for example, to hide or unhide a folder). The easiest way to change folder attributes is to use the following procedure:

  1. Use the GetFolder method to bind to the folder.

  2. Check for the value of the attribute you want to change.

    For example, if you want to unhide a folder, check to see whether the folder is hidden.

  3. If the folder is hidden, use the logical operator XOR to toggle the switch and change it to not hidden. If the folder is not hidden, be careful not to use XOR. If you do, the switch will be toggled, and the folder will end up hidden.

For example, the script in Listing 4.14 uses the AND operator to check whether the switch with the value 2 (hidden folder) has been set on the folder C:\FSO. If it has, the script then uses the XOR operator to turn the switch off and unhide the folder.

Listing 4.14 Changing Folder Attributes


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\FSO")
If objFolder.Attributes AND 2 Then
 objFolder.Attributes = objFolder.Attributes XOR 2
End If