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

How To Extract The Drive Name From A Path In A C++ App

How To Extract The Drive Name from A Path In A C++ App

C++ Builder is the easiest and fastest C and C++ IDE for building a C++ app 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 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.FileCtrlSystem.IOUtilsSystem.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 see how we can use ExtractFileDrive Method to extract a File Drive Name from the File Path String on Windows.

What does the ExtractFileDrive method do?

ExtractFileDrive method (System::SysUtils::ExtractFileDrive) is a Path Manipulation Routine that returns the drive portion of a file name from a path string. In other terms it returns a string containing the drive portion of a fully qualified path name for the file passed in the FileName parameter. For file names with drive letters, the result is in the form ‘<drive>’. For file names with a UNC path (Universal Naming Convention), the result is in the form ‘\\<servername>\<sharedname>’. If the given path contains neither style of path prefix, the result is an empty string. On Linux and MacOS, ExtractFileDrive always returns a zero-length string.

Syntax:

Here is a simple C++ example of how to use the ExtractFileDrive method

We can separate drives and folders in a path string with “\\” to define single \ and we can extract drive name as below,

the output drive string will be “D:”.

Is there a more complete C++ example of using the ExtractFileDrive method?

Here ShowMessage() command will extract and display drive name from this string as “D:” as below,

We can easily use this method with the OpenDialog, SaveDialog components or with the string properties of other components. Here is the C++ Builder VCL Example,

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.

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

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?