Enumerating File Attributes

Microsoft® Windows® 2000 Scripting Guide

Like folders, files also have attributes that can be retrieved and configured using the FileSystemObject. Also like folders, file attributes are returned as a bitmap value. (For more information on bitmap values and how to use them, see "Managing Folder Attributes" earlier in this chapter.) File attributes can include any or all of the values shown in Table 4.6.

Table 4.6 File Attributes Used by the FileSystemObject

Constant

Value

Description

Normal

0

File with no attributes set.

Read-only

1

File can be read but cannot be modified.

Hidden

2

File is hidden from view in Windows Explorer or My Computer.

System

4

File is needed by the operating system.

Archive

32

File is flagged as requiring backup.

Alias

64

File is a shortcut to another file.

Compressed

2048

File has been compressed.

To retrieve the attributes of a file, use the GetFile method to bind to the file. After you have created an object reference to the file, you can use the logical AND operator to determine the file attributes. If the file does not have any attributes configured, the Attributes value will be 0.

For example, the script in Listing 4.28 binds to the file C:\FSO\ScriptLog.txt and then checks for the presence of each attribute that can be retrieved using the FileSystemObject.

Listing 4.28 Enumerating File Attributes

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("C:\FSO\ScriptLog.txt")
If objFile.Attributes AND 0 Then
 Wscript.Echo "No attributes set."
End If
If objFile.Attributes AND 1 Then
 Wscript.Echo "Read-only."
End If
If objFile.Attributes AND 2 Then
 Wscript.Echo "Hidden file."
End If
If objFile.Attributes AND 4 Then
 Wscript.Echo "System file."
End If
If objFile.Attributes AND 32 Then
 Wscript.Echo "Archive bit set."
End If
If objFile.Attributes AND 64 Then
 Wscript.Echo "Link or shortcut."
End If
If objFile.Attributes AND 2048 Then
 Wscript.Echo "Compressed file."
End If