To read the article online, visit http://www.4GuysFromRolla.com/webtech/112600-1.2.shtml

Reading and Altering File Attributes, Part 2


  • Read Part 1

  • In Part 1 we looked at how to read and interpret the Attributes property of the File object (one of the FileSystemObject objects). In this part we'll look at how to use the Attributes property to programmatically alter the attributes for a file on the Web server!

    Recall from the table in Part 1 that there are certain attributes marked and read/write (while others are marked as read-only). As you may have guessed, those marked read/write are values that can be altered by a developer.

    The easiest way to add or remove an attribute from a file is to simply add or subtract the appropriate value from the Attributes property. That is, if you want to make a read/write file read-only, all you have to do is add 1, the value of the Read-Only attribute, to the Attributes property. To toggle off an attribute, simply subtract its value from the Attributes property.

    When using this method there is one huge caveat. If you are going to add some value to the Attributes property (in order to "turn on" a particular attribute), you must first make sure that it does not already possess that value! So, if you wanted to write a function that would make a specified file read-only, you could use the following code:

    Sub MakeReadOnly(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)
    
      'Determine if the file is ALREADY Read-Only
      If objFile.Attributes AND 1 then
        'We are already read-only, do nothing
      Else
        'The file is NOT Read-Only, add 1 to the Attributes property
        objFile.Attributes = objFile.Attributes + 1
      End If
      
    End Sub
    

    That's it! Note that you must check to see if a file has a particular attribute before adding the attribute's value to the Attributes property! Similarly, if you wish to "turn off" an attribute for a given file, you would want to make sure that the file HAD that attribute BEFORE subtracting the attribute's value from the Attributes property. The below subroutine "un-hides" a file (if it is hidden):

    Sub UnHide(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)
    
      'Determine if the file is ALREADY HIDDEN
      If objFile.Attributes AND 2 then
        'The file is hidden, we need to "unhide" it
        objFile.Attributes = objFile.Attributes - 2
      Else
        'The file is NOT HIDDEN, therefore there's no need to unhide it
      End If
      
    End Sub
    

    Well, that about wraps up this article. We examined the Attributes property of the File object and how to use it to read (and modify) a file's various attributes through code. For more information on the Attributes property, be sure to check out the technical docs!

    Happy Programming!


    Article Information
    Article Title: Reading and Altering File Attributes, Part 2
    Article Author: Scott Mitchell
    Published Date: Sunday, November 26, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/112600-1.2.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers