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.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 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:
1 2 3 |
System::UnicodeString __fastcall ExtractFileDrive(const System::UnicodeString FileName); // overload |
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,
1 2 3 |
String drive = ExtractFileDrive( L"D:\\MainFolder\\SubFolder\\myimage.jpg" ); |
the output drive string will be “D:”.
Is there a more complete C++ example of using the ExtractFileDrive method?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { String str = L"D:\\MainFolder\\SubFolder\\myimage.jpg"; String drive = ExtractFileDrive( str ); ShowMessage( L"Drive: " + drive ); } |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <vcl.h> #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) { OpenDialog1->Execute(); String drive = ExtractFileDrive( OpenDialog1->FileName ); ShowMessage( L"Drive: " + drive); } |
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.