How can I get a file name without an extension from a path string in C++ Builder? Can I learn what is GetFileNameWithoutExtension Method is? What is the Syntax of the GetFileNameWithoutExtension Method? Is there an Example to GetFileNameWithoutExtension Method? If I am writing C++ software for Windows, how can I remove the filename’s extension?
C++ Builder has a lot of very useful 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 the drive name, directory name, file name, file extensions, and so on. 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 GetFileNameWithoutExtension Method to get file name from a path string file name on Windows.
Table of Contents
What is the GetFileNameWithoutExtension method?
GetFileNameWithoutExtension (System::IOUtils::TPath:: GetFileNameWithoutExtension) is a Path Manipulation Routine that extracts the name part of a file name, without extension.
GetFileNameWithoutExtension() Method extracts the name part of the given file name, omitting the extension. The resulting string consists of the leftmost characters of FileName, starting with the first character after the colon or backslash that separates the path information from the name and up to the period that is part of the extension, but not including either the dot or the extension itself. If there are more than one period, GetFileNameWithoutExtension stops just before the last one that is considered part of the extension.
What is the syntax of the GetFileNameWithoutExtension method ?
Here is the Syntax of GetFileNameWithoutExtension() Method,
1 2 3 |
UnicodeString __fastcall GetFileNameWithoutExtension(const System::UnicodeString FileName ); |
Here the FileName is the file name from which the name and extension parts should be extracted.
Note: GetFileNameWithoutExtension raises an exception if the given file name contains invalid characters.
Is there a simple example of how to use the GetFileNameWithoutExtension method?
The GetFileNameWithoutExtension method returns a UnicodeString, We can get the file name without the extension from a path string as in the example below,
Example:
Let’s see the filename returned from a full path string of a file.
1 2 3 4 |
String filename = TPath::GetFileNameWithoutExtension( L"D:\\MainFolder\\SubFolder\\MyFile.jpg" ); ShowMessage( filename ); |
and the output from the ShowMessage() function will display the following result:
1 2 3 |
MyFile |
Here is a full example of how to use the GetFileNameWithoutExtension and GetFileName methods
C++ Builder has OpenDialog, SaveDialog, and other components that allow 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 want the filename with its extension (i.e., “.jpg”, “.bmp” with dot) there is a GetFileName() method that gets file name with extension. Its syntax is similar to GetFileNameWithoutExtension method as below,
1 2 3 |
UnicodeString __fastcall GetFileName(const System::UnicodeString FileName); |
Here is the full C++ Builder VCL example of how to use 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 choose the 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