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

How To Get A Directory Name From A Path String

C++ Builder has a lot of specific methods in its SysUtils library that are included in the vcl and fmx libraries. Some of these are grouped as 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.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 drives, files, or directories to see if they exist or not in that given path. And we can get directory names from a given path string.

In this post, you’ll get answers to these questions:

  • How can I get directory name from a path string in C++ Builder?
  • What is GetDirectoryName Method?
  • What is the Syntax of GetDirectoryName Method?
  • Is there an Example to GetDirectoryName Method?

By learning how to use GetDirectoryName Method to get a directory from a path string on Windows, and how to compile c++ in windows. It will help you to easily build C++ applications.

What is the GetDirectoryName Method in Modern C++ ?

GetDirectoryName (System::IOUtils::TPath::GetDirectoryName) is a Path Manipulation Routine that extracts the drive and the first directory part of a path/filename string.

GetDirectoryName extracts the drive and directory parts of the given file name. The resulting string is empty if FileName contains no drive or directory parts. The following table lists the parameters expected by this method.

NameMeaning
FileNameThe file name from which the drive and directory are extracted.

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

What is the Syntax of the GetDirectoryName Method in Modern C++ ?

Here is the Syntax of GetDirectoryName() Method:

Is there a simple example of using the GetDirectoryName Method in Modern C++?

GetDirectoryName Method returns a UnicodeString, we can get drive and directory name from a given path string as 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 path String,

Example 2:

Let’s see return from a path string of a path string that starts with “\”.

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

Example 3:

Let’s see return from a path string of a path string that has folder and file names,

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

Here is a full Example of the GetDirectoryName Method in Modern C++

Here is the full C++ Builder VCL example with the Memo (TMemo) component below,

Use this dynamic link to search for more path manipulation methods: https://learncplusplus.org/?s=path

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.


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

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?