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

How To Get A File Name Without Extension In C++ Software

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.FileCtrlSystem.IOUtilsSystem.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.

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,

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.

and the output from the ShowMessage() function will display the following result:

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,

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,

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.

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.

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++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?