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

Learn To Check If a Drive Really Exists on Windows

In C++ Builder, how can we check if there drive exists in that given string? Can I check a drive if exists or not in C++? Can I check Drive, Folder or a File in C++? Let’s answer your questions.

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.IOUtilsSystem.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.

Let’s see how we can use DriveExists Method to check if it exists in that path on Windows.

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:

PathThe 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:

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,

or we can directly set its result to a components value, for example we can use it with a CheckBox (TCheckBox) component as below

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,

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,

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


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.

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 Generate A Random File Name on Windows

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

How To Include Or Exclude Trailing Backslashes On Windows

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

How To Check Characters of File Names and Paths on Windows

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

How to use SameFileName method on Windows