Search Tools Links Login

Setting and getting file attributes w/o affecting other attributes - Updated

These two simple wrappers can be used for setting and retrieving individual or selected file attributes without affecting the other attributes of the file. For example, to set the Archive bit of a file you should not just set its attributes to vbArchive (32), as this will turn off any other attributes currently set. Normally you would need to get the file attributes, add the desired attribute to the current attributes, then set them again. These wrappers just hide the details of this process. Update thanks to redbird77.
With the GetAttrib wrapper you can easily test the current state of an attribute. You simply specify the attribute(s) and the function will return True if the specified attribute(s) is set to on. You can specify more than one attribute and True will be returned only if all specified attributes are turned on.
The SetAttrib wrapper simplifies the setting of an attribute to on or off, and will be set as desired irrespective of its current state. You can set more than one attibute at a time (eg. SetAttrib(sFile, vbReadOnly Or vbHidden) will set these attributes to on, no matter if they are on or off, without affecting other attributes that may be set.
My first version of the SetAttrib function used 'Xor' to turn attributes off, but thanks to redbird77, I have updated it to 'Not' which has eliminated a limitation of my version. The bottom line is that you can set a files attribute(s) to on or off without needing to know the current state of the specified attributes, while not affecting other attributes that may be set.

Original Author: Rde



' The return value is the sum of the attribute values

Public Declare Function GetAttributes Lib "kernel32" _

    Alias "GetFileAttributesA" (ByVal lpSpec As String) As Long

' Sets the Attributes argument whose sum specifies file attributes

' An error occurs if you try to set the attributes of an open file

Public Declare Function SetAttributes Lib "kernel32" _

    Alias "SetFileAttributesA" (ByVal lpSpec As String, _

    ByVal dwAttributes As Long) As Long

Public Enum vbFileAttributes

  vbNormal = 0
        ' Normal

  vbReadOnly = 1
      ' Read-only

  vbHidden = 2
        ' Hidden

  vbSystem = 4
        ' System file

  vbVolume = 8
        ' Volume label

  vbDirectory = 16
    ' Directory or folder

  vbArchive = 32
      ' File has changed since last backup

  vbTemporary = &H100
 ' 256

  vbCompressed = &H800
' 2048

End Enum

Public Function GetAttrib(sFileSpec As String, ByVal Attrib As vbFileAttributes) As Boolean

  ' Returns True if the specified attribute(s) is currently set.

  GetAttrib = (GetAttributes(sFileSpec) And Attrib) = Attrib

End Function

Public Sub SetAttrib(sFileSpec As String, ByVal Attrib As vbFileAttributes, Optional fTurnOff As Boolean)

  ' Sets/clears the specified attribute(s) without affecting other attributes. You

  ' do not need to know the current state of an attribute to set it to on or off.

  Dim Attribs As Long

  Attribs = GetAttributes(sFileSpec)

  If fTurnOff Then

      SetAttributes sFileSpec, Attribs And (Not Attrib)


      SetAttributes sFileSpec, Attribs Or Attrib

  End If

End Sub

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 87 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.