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

How To Use GetFileName On A Path String In A C++ App

How To Use GetFileName On A Path String In A C++ App

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.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 the GetFileName Method to get the file name from a path string file name in a Windows C++ app.

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:

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.

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

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,

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,

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.

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++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond