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

How To Use The ExtractFileName Method In Your C++ Software

How To Use The ExtractFileName Method In Your C++ Software

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional C++ Software 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.

C++ Builder has specific path manipulation routines that allows user to edit, extract, get and set drive name, directory name, file name, file extensions etc. and these methods are combined in Vcl.FileCtrlSystem.IOUtilsSystem.SysUtils libraries. These path manipulation 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 recently discussed how to use the GetFileName method in your C++ app in an article on this blog. The ExtractFileName is very similar, but works in a slightly different way.

Let’s see how we can use ExtractFileName Method to extract a File Name from the File Path String on Windows.

What is the ExtractFileName method?

The ExtractFileName method (System::SysUtils::ExtractFileName) is a Path Manipulation Routine that extracts the name and extension parts of a file name from a path string. In other terms it returns a string that is the rightmost characters of FileName, starting with the first character after the colon or backslash that separates the path information from the name and extension. The resulting string is equal to FileName, if FileName contains no drive and directory parts. Note that this function works for multi-byte character systems (MBCS) that means it uses and returns UnicodeString.

Syntax:

Simple Example:

We can separate drives and folders in a path string with “\\” to define single \ and we can extract file name as below,

the output file name string will be ” myimage.jpg “.

Here is a full C++ Builder VCL example of how to use ExtractFuileName in your C++ software

Here ShowMessage() command will extract and display file name from this string as “myimage.jpg” below,

How To Use The ExtractFileName Method In Your C++ Software - a dialog box showing the an extracted file name

We can easily use this method with the FileName property of OpenDialog, SaveDialog components or with the string properties of other components. Here is the C++ Builder VCL Example,

RAD Studio C++ Builder is a great environment for learning to use C++ and is also powerful enough for all your professional and beginner development needs.
You can download and use the latest C++ Builder Community Edition free.
You can download and try the C++ Builder trial version 30 days.

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

What Is Count In C Programming?

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

Why Is C The Most Popular Programming Language?

CC++C++17Introduction to C++Learn C++Syntax

How To Use Switch In C++ And C Programming?

CC++C++17Introduction to C++Learn C++

What Are Header Files In C++ and C Programming?