C++ Builder has a lot of specific methods in its SysUtils library that is included in vcl and fmx libraries. Some of these are grouped as a 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 drive, files or directories if they are exist or not in that given path.
In this post, you’ll learn what C++ software can do, how to check if a drive exists in a given string, and how to check if a drive exists or not in C++. Whether it is possible to check Drive, Folder, or File in C++. By learning more about the DriveExists 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 is the C++ DriveExists method?
DriveExists Method (System.IOUtils.TPath.DriveExists) is a IOUtils Path Method in C++ Builder that Checks whether the drive letter used in the given path actually exists. Call DriveExists to check whether a path’s drive letter identifies a valid Windows drive. DriveExists() method returns True if the path’s root is a drive letter that identifies a valid drive, or False otherwise.
The following table lists the parameters expected by this method:
Name | Meaning |
---|---|
Path | The verified path |
Note that On POSIX, DriveExists will always returns false
and also DriveExists raises an exception if the given paths contain invalid characters.
What is the syntax of the C++ DriveExists method?
Here is the Syntax of DriveExists Method:
1 2 3 |
static bool __fastcall DriveExists(const System::UnicodeString Path); |
Here is a simple example of the C++ DriveExists() method
DriveExist Method returns a Boolean, we can check a file and set the result to a bool as below,
1 2 3 |
bool check = DriveExists( L"D:" ); |
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 = DriveExists( L"D:" ); |
Note that, we can use “D:” oır “D:\\” strings to check D drive but we can not use “D” one letter string alone, this time it will return false while it exits. Also drive names i.e “MyWindows” or “MyWindows:” doesn’t work with this method.
Here’s a full Example of using the C++ DriveExists() method
Here is the example how we can check if a drive 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 ( TPath::DriveExists( L"C:\\") ) { ShowMessage("C: Drive exists"); } else { ShowMessage("C: Drive does NOT exist"); } } |
As same as here above, there are a DirectoryExists() method to check directories and DriveExists() method to check files. More examples about Path Operations 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 |
#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 ( TPath::DriveExists( L"C:\\") ) { ShowMessage("C: Drive exists"); } else { ShowMessage("C: Drive does NOT exist"); } if ( DirectoryExists( L"C:\\Windows\\System32") ) { ShowMessage("Systme32 Folder exists"); } else { ShowMessage("Systme32 Folder doesn't exist"); } 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.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, we have Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here