article

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

Email
Submitted on: 2/9/2015 8:46:00 AM
By: Rde (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 3160
 
     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.

 
				

 

' 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.
  If (LenB(sFileSpec) <> 0) Then
    GetAttrib = (GetAttributes(sFileSpec) And Attrib) = Attrib
  End If
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.

  If (LenB(sFileSpec) <> 0) Then
    If (Attrib = vbNormal) Then
      SetAttributes sFileSpec, vbNormal
    ElseIf fTurnOff Then
      SetAttributes sFileSpec, GetAttributes(sFileSpec) And (Not Attrib)
    Else
      SetAttributes sFileSpec, GetAttributes(sFileSpec) Or Attrib
    End If
  End If
End Sub



Other 30 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.