Published: Sunday, November 26, 2000
Reading and Altering File Attributes
One of the most useful (and coolest) Microsoft-provided components for use with ASP pages is the
FileSystemObject. This object, as its name implies, allows programmatic access to the Web server's filesystem.
There are a number of objects that are included with FileSystemObject, such as the File, Drive, and Folder
objects, which contain specific information about a particular aspect of the filesystem. If you are
unfamiliar with the FileSystemObject and its related objects, I highly suggest that you check out the
following resources before continuing on with this article:
There are a number of good articles on the FileSystemObject here on 4Guys and all over the Net;
regardless, there are certain properties of various FileSystemObjects that don't get much press. One of those
is the Attributes property of the File object, which can be used to read and modify the
various attributes of a file on the Web server. The file attributes revealed by the Attributes
property include whether a file is Read-Only, a System File, a Hidden File, a directory, if it's compressed,
if it's archived, etc.
The Attributes property compresses all of this information about a file into a numeric value ranging
from zero to 255. For example, the following code produces a value of 32:
<%
'Create an instance of the FileSystemObject
Dim objFSO, objFile
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Use the GetFile method to get information on a file
Set objFile = objFSO.GetFile(Server.MapPath("/index.shtml"))
'Output its attributes value
Response.Write objFile.Attributes
Set objFile = Nothing
Set objFSO = Nothing
%>
|
[
View the Live Demo!]
I'm sure you're wondering what the hell that means! :-) Well, to understand what various
Attribute values mean, we have to consult the following table:
Attribute Values |
| Attribute | Value | Description |
| Normal | 0 | No attributes set. |
| Read-Only | 1 | Read-only file. (Attribute is read/write.) |
| Hidden | 2 | File is a hidden file. (Attribute is read/write.) |
| System | 4 | A system file. (Attribute is read/write.) |
| Volume | 8 | Disk drive volume label. (Attribute is read-only.) |
| Directory | 16 | The file is a directory. (Attribute is read-only.) |
| Archive | 32 | File needs to be archived (i.e., it has changed since the last backup). (Attribute is read/write.) |
| Alias | 64 | The file is a shortcut (link). (Attribute is read-only.) |
| Compressed | 128 | File is compressed. (Attribute is read-only.) |
So, in the demo example, /index.shtml has only one
of these properties set, Archive. If it had also been, say, Read-Only, then the value of the Attributes
property would be the sum of the two properties (33). So, to determine a given file has
a given attribute, you can use the bit-wise AND (AND) operator like so:
If (objFile.Attributes AND AttributeValue) then
'The attribute value is set...
Else
'The attribute value is not set...
End If
|
The code below provides a function to view the file attributes for a given file. There's also a
live demo to try out. In Part 2
we'll examine how to alter a file's attributes programmatically!
Read Part 2!
Sub ListAttributes(strFileName)
'Create an instance of the FileSystemObject
Dim objFSO, objFile
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Use the GetFile method to get information on a file
Set objFile = objFSO.GetFile(strFileName)
Response.Write "The <code>Attributes</code> property: " & _
objFile.Attributes & "<p>"
'List out its properties:
if objFile.Attributes = 0 then
Response.Write "No Attributes Set..."
Else
'File has attributes, so find them out!
If objFile.Attributes AND 1 then
Response.Write "File is Read-only<br>"
Else
Response.Write "File is <b>not</b> Read-only<br>"
End If
If objFile.Attributes AND 2 then
Response.Write "File is a hidden file<br>"
Else
Response.Write "File is <b>not</b> a hidden file<br>"
End If
If objFile.Attributes AND 4 then
Response.Write "File is a system file<br>"
Else
Response.Write "File is <b>not</b> a system file<br>"
End If
If objFile.Attributes AND 8 then
Response.Write "File is a disk drive volumn label<br>"
Else
Response.Write "File is <b>not</b> a disk drive volumn label<br>"
End If
If objFile.Attributes AND 16 then
Response.Write "File is a directory<br>"
Else
Response.Write "File is <b>not</b> a directory<br>"
End If
If objFile.Attributes AND 32 then
Response.Write "File has the Archive bit set<br>"
Else
Response.Write "File does <b>not</b> have the Archive bit set<br>"
End If
If objFile.Attributes AND 64 then
Response.Write "File is a link<br>"
Else
Response.Write "File is <b>not</b> a link<br>"
End If
If objFile.Attributes AND 128 then
Response.Write "File is compressed<br>"
Else
Response.Write "File is <b>not</b> not compressed<br>"
End If
End If
End Sub
|
[View the Live Demo!]
Read Part 2!