When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
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
AttributeValueDescription
Normal0No attributes set.
Read-Only1Read-only file. (Attribute is read/write.)
Hidden2File is a hidden file. (Attribute is read/write.)
System4A system file. (Attribute is read/write.)
Volume8Disk drive volume label. (Attribute is read-only.)
Directory16The file is a directory. (Attribute is read-only.)
Archive32File needs to be archived (i.e., it has changed since the last backup). (Attribute is read/write.)
Alias64The file is a shortcut (link). (Attribute is read-only.)
Compressed128File 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!



  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article