C++Language FeatureLearn C++

Everything You Need To Set File Attributes In Your Apps

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. There is a free C++ Builder Community Edition for students, beginners, and startups.

C++ Builder has Path Manipulation Routines that allow users to edit, extract, get and set drive name, directory name, file name, and file extensions. These methods are found in the Vcl.FileCtrl, System.IOUtils, System.SysUtils libraries. These path manipulation methods are easy to use and with them it’s easy to get or set file path strings in that operating system. The routines can be used with other component properties like FileName property of OpenDialog, SaveDialog components.

Let’s see how we can use SetAttributes Method to set File Attributes from given a file or directory path String on Windows.

What is the SetAttributes method?

The SetAttributes method (System::SysUtils::SetAttributes) is a Path Manipulation Routine that sets the file or directory attributes. We can call the SetAttributes method to apply a new set of attributes to a given file or directory.

Syntax:

Here;
The Path is the path to the file or directory for which the attributes are obtained.
The Attributes is the set of file or directory attributes.

Is there a C++ example of how to set file attributes?

Of course! Here’s a simple example to of how to set file attributes.

We can separate drives and folders in a path string with “\\” to define single \ and we can get attributes of a given file path string as below,

As you see in this example, we can easily set the attribute ReadOnly to ON. In C++ Builder, we can set this ReadOnly flag to OFF as example below

What file attributes are available and what do they mean?

TFileAttributes represents a set of file or directory attributes. It is a set of file or directory attributes. The TFileAttributes enumeration is used in file operation routines, which modify, read, or remove attributes from a file or directory. The following are possible values of TFileAttribute:

ValueMeaning
faReadOnlyIdentifies read-only files or directories.
faHiddenIdentifies hidden files or directories.
faSystemIdentifies system files or directories.
faDirectoryIdentifies a directory.
faArchiveIdentifies Windows archived files.
faDeviceIdentifies Windows device files.
faNormalIdentifies normal files.
faTemporaryIdentifies temporary files or directories.
faSparseFileIdentifies a sparse file. A sparse file is a large file filled mostly with zeros.
faReparsePointIdentifies a reparse point. A reparse point is a block of user-defined data linked to a real file or directory.
faCompressedIdentifies a compressed file or directory.
faOfflineIdentifies an offline file whose contents are unavailable.
faNotContentIndexedIdentifies a file that is skipped from the indexing operations.
faEncryptedIdentifies an encrypted file or directory.
faSymLinkIdentifies a symbolic link.

Note that there are TFileAttributes enumeration and TFileAttribute without plural s. One defines all atributes other defines defined attribute (i.e. TFileAttribute::ReadOnly). So be careful about if it is singular or plural when you are using them, because both has different usage.

This method is very useful when you want to set file attributes. Here is the simple full C++ Builder VCL Example,

We can use this method with path properties of other components. For example, here is a full example with OpenDialog componant that uses it’s FileName property and switches this file ReadOnly flag to ON or OFF,

In this example, user selects file by using OpenDialog file browser and it sets its ReadOnly to OFF if it is ReadOnly vice verse. Note that you can use ^= bitwise operator.to switch bits too.

RAD Studio C++ Builder is a great environment for learning to use C++ and is also powerful enough for all your professional and beginner development needs.
You can download and use the latest C++ Builder Community Edition for free subject to some conditions. If you want to go even further or don’t qualify for the Community Edition why not download and try the C++ Builder trial version?

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Download A Modern C++ Compiler for Windows 10 And 11

Artificial Intelligence TechC++Introduction to C++Learn C++

How To Make A Simple But Powerful Chat Bot In C++

C++C++17Introduction to C++Language FeatureLearn C++

How To Operate On The Words In A String In C++

C++Introduction to C++Language FeatureLearn C++

What Is The Best C++ Compiler for Windows 10?

en_USEnglish