C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications for Windows App Development, 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 specific Path Manipulation Routines that allows user to edit, extract, get and set drive name, directory name, file name, file extensions, .. etc and these methods are combined in Vcl.FileCtrl, System.IOUtils, System.SysUtils libraries. These path manipulation methods are easy to use and easy to get or set file path strings in that operating system. These can be used with other component properties like FileName property of OpenDialog, SaveDialog components.
Let’s see how we can use GetAttributes Method to get File Attributes from given a file or directory path String on Windows.
What does the GetAttributes method do?
The GetAttributes method (System::SysUtils::ExtractFileName) is a Path Manipulation Routine that used to obtain the attributes of a given file. The return value of GetAttributes is a set of TFileAttributes values; each value of the set represents a file attribute. To use this GetAtrributes method we need to include System.IOUtils.hpp.
Note that GetAttributes method raises an exception if the file or directory cannot be accessed or the path is invalid. If the Path parameter is a symbolic link and the FollowLink parameter is set to True, the method is performed on the target file (or directory). If the first condition is True, but the FollowLink parameter is set to False, the method will be performed on the symbolic link. If the link is broken, the method will always return False.
Syntax:
1 2 3 |
static TFileAttributes __fastcall GetAttributes(const System::UnicodeString Path, bool FollowLink = true); |
Here;
Path is the path to the file or directory for which the attributes are obtained.
FollowLink specifies whether the symbolic link is used.
Simple Example:
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,
1 2 3 4 5 |
TFileAttributes attr = TFile::GetAttributes( L"D:\\MainFolder\\SubFolder\\myimage.jpg" ); if( attr.Contains(TFileAttribute::faReadOnly) ) ShowMessage("Read Only"); |
As you see in this example, we can easily check if the file is ReadOnly or not.
What is the TFileAttributes set?
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:
Value | Meaning |
---|---|
faReadOnly | Identifies read-only files or directories. |
faHidden | Identifies hidden files or directories. |
faSystem | Identifies system files or directories. |
faDirectory | Identifies a directory. |
faArchive | Identifies Windows archived files. |
faDevice | Identifies Windows device files. |
faNormal | Identifies normal files. |
faTemporary | Identifies temporary files or directories. |
faSparseFile | Identifies a sparse file. A sparse file is a large file filled mostly with zeros. |
faReparsePoint | Identifies a reparse point. A reparse point is a block of user-defined data linked to a real file or directory. |
faCompressed | Identifies a compressed file or directory. |
faOffline | Identifies an offline file whose contents are unavailable. |
faNotContentIndexed | Identifies a file that is skipped from the indexing operations. |
faEncrypted | Identifies an encrypted file or directory. |
faSymLink | Identifies 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.
Is there a simple Windows app development C++ example of getting a file’s attributes?
This method is very useful when you want to check file attributes. Here is the simple full C++ Builder VCL Example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <vcl.h> #include <System.IOUtils.hpp> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { try { TFileAttributes attr = TFile::GetAttributes(str); if( attr.Contains(TFileAttribute::faReadOnly) ) ShowMessage("Read Only"); else ShowMessage("Read and Write"); } catch(Exception &e) { throw(e); // Throw errors like "File not found", "is not a file" etc.. } } |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <vcl.h> #include <System.IOUtils.hpp> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { if(OpenDialog1->Execute()) { try { TFileAttributes attr = TFile::GetAttributes(OpenDialog1->FileName); if( attr.Contains(TFileAttribute::faReadOnly) ) ShowMessage( "Read Only" ); else ShowMessage( "Read and Write" ); } catch( Exception &e ) { throw(e); } } } |
In this example, user selects file by using OpenDialog file browser and it tries to check if this file attribute has faReadOnly feature.
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 free.
You can download and try the C++ Builder trial version 30 days.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition