How can I get a file name from a path string in a C++ app using C++ Builder? Can I learn what is the GetFileName Method? What is the syntax of the GetFileName Method? Is there an example of using the GetFileName Method?
C++ Builder has a lot of specific methods in its SysUtils and IOUtils library that are included in VCL and FMX libraries. Some of these are grouped as path manipulation routines that allow users to edit, extract, get and set drive name, directory name, file name, and file extensions. 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 drives, files, or directories if they exist or not in that given path. And we can get file name from a given path string.
Let’s see how we can use the GetFileName Method to get the file name from a path string file name in a Windows C++ app.
Table of Contents
What is the GetFileName Method?
GetFileName (System::IOUtils::TPath::GetFileName) is a path manipulation routine that extracts and returns the name and extension parts of a file name specified in FileName. GetFileName extracts the name and extension parts of the file name given in FileName. The resulting string consists of the characters of FileName, starting with the first character after the colon or backslash that separates the path information from the file name and extension. The resulting string is equal to FileName if FileName contains no drive and directory parts.
What is the syntax of the GetFileName method?
Here is the syntax of GetFileName() method:
1 2 3 |
UnicodeString __fastcall GetFileName(const System::UnicodeString FileName); |
Here the FileName is the file name from which the name and extension parts should be extracted.
Note: GetFileName raises an exception if the given FileName contains invalid characters.
A simple example of using the GetFileName method
GetFileName Method returns a UnicodeString, We can get file name and it’s extension from a path string as in example below,
Example:
Let’s see filename return from a full path string of a file.
1 2 3 4 |
String filename = TPath::GetFileName( L"D:\\MainFolder\\SubFolder\\MyFile.jpg" ); ShowMessage( filename ); |
and the output form of ShowMessage() will display this result file name String,
1 2 3 |
MyFile.jpg |
A full example of how to use the GetFileName and GetFileNameWithoutExtension methods in your C++ app
C++ Builder has OpenDialog, SaveDialog, and other components that allows you browse files by a File Browser to save or to load a file. We can use GetFileName() method to get file name from FileName property of these Dialogs.
If you dont want to get extension (i.e. “.jpg”, “.bmp” with dot) there is a GetFileNameWithoutExtension() method that gets file name without extension. It’s syntax is similar to GetFileName method as below,
1 2 3 |
UnicodeString __fastcall GetFileNameWithoutExtension(const System::UnicodeString FileName ); |
Here is the full C++ Builder VCL example to GetFileName() and GetFileNameWithoutExtension() Methods with the OpenDialog (TOpenDialog) component that gets file name with extension and without extension. See example below,
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 |
#include <vcl.h> #include <IOUtils.hpp> #pragma hdrstop #include "Get_Methods_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { if( OpenDialog1->Execute() ) { String filename_ex = TPath::GetFileName( OpenDialog1->FileName ); ShowMessage( filename_ex ); String filename = TPath::GetFileNameWithoutExtension( OpenDialog1->FileName ); ShowMessage( filename ); } } |
If you choice same file example above, this example will show “MyFile.jpg” by the GetDileName() Method and “MyFile” by the GetFileNameWithoutExtension() Method.
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; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition