C++ Builder has specific Path Manipulation Routines that allows your C++ app to edit, extract, get and set drive name, directory name, file name, file extensions and so on. 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’s see how we can use ExtractFileExt Method to extract a File Extension from the File Path String on Windows.
What does the ExtractFileExt method do?
The ExtractFileExt method (System::SysUtils::ExtractFileName) is a Path Manipulation Routine that returns the extension portions of a file name. We can use ExtractFileExt to obtain the extension of a file name.
The resulting string includes the period character that separates the name and extension parts. This string is empty if the given file name has no extension. Note that this function works for multi-byte character systems (MBCS) ) that means it uses and returns UnicodeString.
Syntax:
1 2 3 |
System::UnicodeString __fastcall ExtractFileExt(const System::UnicodeString FileName); // overload |
Here is a simple example of how to use ExtractFileExt
We can separate drives and folders in a path string with “\\” to define single \ and we can extract file extension as below.
1 2 3 |
String fileext = ExtractFileExt( L"D:\\MainFolder\\SubFolder\\myimage.jpg" ); |
the output file name string will be “.jpg ” including dot character.
Here is the simple full C++ Builder VCL Example,
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 fileext = ExtractFileExt( str ); ShowMessage( L"File Extension: " + fileext); } |
Here ShowMessage() command will extract and display file name from this string as “.jpg” below,
We can easily use this method with the FileName property of 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 fileext = ExtractFileExt( OpenDialog1->FileName ); ShowMessage( L"File Extension: " + fileext); } |
This will show the extension of a file that you select by the OpenDialog component.
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition