C++ Builder has a lot of specific methods in its SysUtils library that are included in the vcl and fmx libraries. Some of these are grouped as 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 all 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. We can also check drives, files, or directories to see if they exist or not in that given path.
In this post, you’ll learn how to check if a directory exists in a given path; how to check if a folder exists or not in C++, and how to check a Drive, Directory, or File in C++. By learning more about the DirectoryExists Method that checks if it exists in that path on Windows, and how to compile c++ in Windows. It will help you to easily build C++ applications.
Table of Contents
What does the C++ DirectoryExists method do?
DirectoryExists Method (System::SysUtils::DirectoryExists) is a SysUtils Method that determines whether a specified directory exists. Call DirectoryExists() to determine whether the directory specified by the Directory
parameter exists. If the directory exists, the function returns True. If the directory does not exist, the function returns False. If a full path name is entered, DirectoryExists searches for the directory along the designated path. Otherwise, the Directory
parameter is interpreted as a relative path name from the current directory.
What is the syntax of the C++ DirectoryExists method?
Here is the Syntax of DirectoryExists Method:
1 2 3 |
bool __fastcall DirectoryExists(const System::UnicodeString Directory, bool FollowLink = true); |
Note: If the Directory parameter is a symbolic link and the FollowLink parameter is set to True, the method is performed on the target 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.
Note: The Vcl.FileCtrl unit (Windows only) also contains a DirectoryExists function. However, the Vcl.FileCtrl version is deprecated, and the System.SysUtils version is preferred and can be used in multi-device applications.
Method behavior if the Directory parameter is a directory:
Target Exists | FollowLink | Method result |
---|---|---|
YES | True | True |
YES | False | True |
NO | True | False |
NO | False | False |
Method behavior if the Directory parameter is a symbolic link:
Target Exists | FollowLink | Method result |
YES | True | True |
YES | False | True |
NO | True | False |
NO | False | True |
A simple Example of how to use the C++ DirectoryExists method
DirectoryExist Method returns a Boolean, we can check a file and set the result to a bool as below,
1 2 3 |
bool check = DirectoryExists( L"D:\\MainFolder\\SubFolder" ); |
or we can directly set its result to a components value, for example we can use it with a CheckBox (TCheckBox) component as below
1 2 3 |
CheckBox1->IsChecked = DirectoryExists( L"D:\\MainFolder\\SubFolder" ); |
Here’s a full example of how to use the C++ DirectoryExists method
Here is the example how we check if a directory exists or not,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <vcl.h> #include <IOUtils.hpp> #pragma hdrstop #include "SysUtils_File_Methods_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { if ( DirectoryExists( L"C:\\Windows\\System32") ) { ShowMessage("Systme32 Folder exists"); } else { ShowMessage("Systme32 Folder doesn't exist"); } } |
As same as here above, there are a FileExists() method to check directories and DriveExists() method to check files. More examples about Path Operations can be found here and Disk and Directory Support Routines can be found here
Here is the full example about using DriveExists(), DirectoryExists() and FileExists() Methods,
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <vcl.h> #include <IOUtils.hpp> #pragma hdrstop #include "SysUtils_File_Methods_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { // CHECK DRIVE if ( TPath::DriveExists( L"C:") ) { ShowMessage("C: Drive exists"); } else { ShowMessage("C: Drive does NOT exist"); } // CHECK DIRECTORY if ( DirectoryExists( L"C:\\Windows\\System32") ) { ShowMessage("Systme32 Folder exists"); } else { ShowMessage("Systme32 Folder doesn't exist"); } // CHECK FILE if ( FileExists( L"C:\\Windows\\System32\\NotePad.exe") ) { ShowMessage("NotePad.exe File exists in that path"); } else { ShowMessage("NotePad.exe File doesn't exist in that path"); } if ( FileExists( L"C:\\Windows\\System32\\LearnCPlusPlus.exe") ) { ShowMessage("LearnCPlusPlus.exe File exists in that path"); } else { ShowMessage("LearnCPlusPlus.exe File doesn't exist in that path"); } } |
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.