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

How To Get A File Extension From A Windows Path In A C++ App

How To Get A File Extension From A Windows Path In A C++ App

How can I get file extension from a path string in a C++app on Windows PC? What is the GetExtension Method? What is the syntax of the GetExtension method? Is there an example of how to use the GetExtension Method?

The C++ Builder IO libraries

C++ Builder has a lot of specific methods in its SysUtils and IOUtils library that is included in vcl and fmx libraries. Some of these are grouped as a Path Manipulation Routines that allows user to edit, extract, get and set drive name, directory name, file name, file extensions, and these methods are combined in Vcl.FileCtrl, System.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 the drive, files, or directories to see if they exist or not in that given path. And we can get file extension from a given path string.

Let’s see how we can use GetExtension Method to get extension of a file from a path string file name on Windows.

What is the GetExtension method?

GetExtension (System::IOUtils::TPath::GetExtension) is a Path Manipulation Routine that extracts the extension part from a path string file name. GetExtension extracts the extension part of the given file name. The resulting string also includes the dot that separates the extension from the path. If FileName has no extension, the resulting string is empty. The following table lists the parameters expected by this method.

NameMeaning
FileNameThe file name from which the extension is extracted.

Note: GetExtension raises an exception if the given file name contains invalid characters.

What is the syntax of the GetExtension method?

Here is the Syntax of GetExtension() Method:

A simple example of using the GetExtension method in a C++ app

The GetExtension Method returns a UnicodeString, We can get the file extension from a path string as in example below,

Example 1:

Let’s see return from a full path string of a file.

and the output form of ShowMessage() will display this result extension String,

Here is a more complete C++ app example of using the GetExtension method

C++ Builder has OpenDialog, SaveDialog, and other components that allow you browse files by a File Browser to save or to load a file. These dialogs also have a Filter Property, so you can filter listed files in given extensions. Nevertheless, some users may force filename extensions in their mind. Thus you can use GetExtension() method to check FileName property of these Dialogs.

Here is the full C++ Builder VCL example with the OpenDialog (TOpenDialog) component that checks if selected filename is “.jpg” or “.png” in any case format. See example below,

This example can be used to limit users in different filetypes. For example, it can be used to upload images to a database, some files take a lot of size (for example .bmp), so you can limit users with .png, .jpg, .gif files. This will help you a lot to speed up your other database operations on these images.

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.

close

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

How To Use WString find_first_of() In A Modern C++ App

C++Learn C++

How To Read An XML File In A C Program Or C++ App

C++Learn C++

Solve A 3 Diagonal Matrix Using Successive Over Relaxation

C++C++11C++14C++17Introduction to C++Learn C++

How To Make Use Of Wide String Properties In C++ Software