Advanced Files and Folders in WSH - Examining File Attributes
(Page 3 of 4 )
The FileSystemObject also allows us to view and edit a file or folder’s properties. This can be very helpful for error-checking before trying to modify a file or folder since a read-only attribute will result in an error.
Value | Attribute |
0 | None |
1 | Read-only |
2 | Hidden |
4 | System |
32 | Archive |
64 | Shortcut |
2048 | Compressed |
The File and Folder objects both have an Attributes property that contains an integer representing the file or folder’s attribute settings. This integer is obtained by adding the constant value in the table to the left for each attribute that is applied to the file.
For example, a Read-only System file would have an attribute value of 5 by adding 1 for Read-only and 4 for System.
Let’s put this to real-world use. In your root folder is a file called boot.ini. This is a Windows configuration file that contains the contents of the Advanced Boot Menu. It typically has Read-only, Hidden, System, and Archive attributes giving it an attribute value of 39. Take a look at the following code.
Const NO_ATTRIBUTES = 0
Const READ_ONLY = 1
Const HIDDEN = 2
Const SYSTEM = 4
Const ARCHIVE = 32
Const LNK_SHORTCUT = 64
Const COMPRESSED = 2048
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.GetFile("C:boot.ini")
Wscript.Echo "The file " & objFile.Path & " with attribute code " _
& objFile.Attributes & " has the following attributes:"
If objFile.Attributes AND NO_ATTRIBUTES Then
Wscript.Echo "No attributes"
End If
If objFile.Attributes AND READ_ONLY Then
Wscript.Echo "Read-only"
End If
If objFile.Attributes AND HIDDEN Then
Wscript.Echo "Hidden"
End If
If objFile.Attributes AND SYSTEM Then
Wscript.Echo "System"
End If
If objFile.Attributes AND ARCHIVE Then
Wscript.Echo "Archive"
End If
If objFile.Attributes AND LNK_SHORTCUT Then
Wscript.Echo "Shortcut"
End If
If objFile.Attributes AND COMPRESSED Then
Wscript.Echo "Compressed"
End If
In the beginning of our code we set some constants for readability purposes. Their values correspond to the attribute values from the table above. We begin by connecting to the FileSystemObject and getting a handle on our file.
Then, we Echo the current file information. Finally, a series of If statements checks to see if each of the attributes exist. Notice that we check our values from smallest to largest. The code should have an output similar to the following:
The file C:boot.ini with attribute code 39 has the following attributes:
Read-only
Hidden
System
Archive
Next: Changing File Attributes >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer