VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



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

by Rde (54 Submissions)
Category: Files/File Controls/Input/Output
Compatability: VB Script
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

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.

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


 


' 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




Download this snippet    Add to My Saved Code

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

No comments have been posted about Setting and getting file attributes w/o affecting other attributes Updated. Why not be the first to post a comment about Setting and getting file attributes w/o affecting other attributes Updated.

Post your comment

Subject:
Message:
0/1000 characters