top of page

Setting file / folder attributes

  • Writer: John
    John
  • 51 minutes ago
  • 2 min read
This post is one of a series providing implementation examples of Windows API Functions, Types, Enums and Consts using VBA. The code in this post can be used as-is, however, if you regularly (or even just occasionally) work with Windows API declarations in VBA, you may want to see the posts Automatically add Windows API declaration(s) and Using 'F1' to view Windows API web pages which explain some of the functionality that can be added to the VBE by VBE_Extras.

This post follows on from the Getting file / folder attributes post. If you haven't already, I suggest you read that post first.


Using the SetAttr VBA statement you can set various attributes for a file or folder including whether it is read-only, hidden or a system file. You can set more attributes using the Attributes property of the FileSystemObject. But for even more attributes, you can use the SetFileAttributes Windows API Function which will set any of the attributes listed on the SetFileAttributes page (note that this is only a sub-set of the attributes that a file or folder can have, a full set of the attributes can be seen here File Attributes Constants) which are summarised in this Enum (see the web page for the meaning of each attribute) ...



The code to set specific attributes is quite simple, as follows (in addition to the above Enum) ...



This can then be tested with the following code ...



As per the code comment, you will need to create a "SomeTextFile.txt" file on your Desktop first. Optionally change FILE_ATTRIBUTE_TEMPORARY to another Enum member (ie attribute) to test the code. You can also set multiple attributes using 'Or' eg FILE_ATTRIBUTE_TEMPORARY Or FILE_ATTRIBUTE_HIDDEN.


If you want to print out, to the Immediate window, which attributes are set having run the above code, see the code provided in the Getting file / folder attributes post.


This code suffers from an obvious issue ... namely, what if you want to just update the existing attribute(s) instead of setting fresh attribute(s)? The solution, of course, is to update the code to read the existing attributes and then apply (or remove) the required attributes from those already present.


First, the GetFileAttributes Windows API Function and the INVALID_FILE_ATTRIBUTES Const must be added ... this makes the Windows API declarations as follows ...



Then some code to update the attributes, and code to get the existing attributes (note that TryGetAttributes, below, is the same as in the Getting file / folder attributes post) ...



This can then be tested with the following code ...



If you have not already you will need to create a "SomeTextFile.txt" file on your Desktop first. Again, for testing you can change FILE_ATTRIBUTE_TEMPORARY to another Enum member (ie attribute) and you can update multiple attributes at once using 'Or' eg FILE_ATTRIBUTE_TEMPORARY Or FILE_ATTRIBUTE_HIDDEN. To set attribute(s) pass True for the bSet parameter and to remove attribute(s) pass False.


Note that FILE_ATTRIBUTE_NORMAL cannot be set or removed directly using TryUpdateAttributes (it can be set directly using TrySetAttributes). This is due to the nature of the FILE_ATTRIBUTE_NORMAL attribute, it being the attribute that is set when no other attributes are set. To set the FILE_ATTRIBUTE_NORMAL attribute using TryUpdateAttributes, you should just remove all other currently-set attributes.


bottom of page