Configuring File Attributes

Microsoft® Windows® 2000 Scripting Guide

In addition to enumerating file attributes, the FileSystemObject provides a way to configure the following attributes:

  • ReadOnly

  • Hidden

  • System

  • Archive

To configure a file attribute, the script should use the following procedure:

  1. Use the GetFile method to bind to the file.

  2. Check for the attribute you want to change.

    For example, if you want to make a file read-only, check to see whether the file has already been marked read-only.

  3. If the file is not read-only, use the logical operator XOR to toggle the switch. This will mark the file as read-only. If the file is already read-only, be careful not to use XOR. If you do, the switch will be toggled, and the read-only attribute will be removed.

The script in Listing 4.29 uses the AND operator to check whether the switch with the value 1 (read-only) has been set on the file C:\FSO\TestScript.vbs. If the file is not read-only, the script uses the XOR operator to turn the switch on and mark the file as read-only.

Listing 4.29 Configuring File Attributes

  
1
2
3
4
5
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("C:\FSO\TestScript.vbs")
If objFile.Attributes AND 1 Then
 objFile.Attributes = objFile.Attributes XOR 1
End If

You can also simultaneously remove the ReadOnly, Hidden, System, and Archive attributes by using the following code statement:

objFile.Attributes = objFile.Attributes AND 0