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

How To Extract The File Directory Name From A File Path In C++

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.

C++ Builder has specific Path Manipulation Routines that allows 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. The path manipulation methods are easy to use and simplify the process of getting or setting file path strings. These can be used with other component properties like FileName property of OpenDialog, SaveDialog components.

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

ExtractFileDir Method

The ExtractFileDir method (System::SysUtils::ExtractFileDir) is a Path Manipulation Routine that extracts the drive and directory parts from the FileName. The resulting string is a directory name suitable for passing to the CreateDirGetCurrentDirRemoveDir, and SetCurrentDir functions. This string is empty if FileName contains no drive and directory parts. Note that this function works for multi-byte character systems (MBCS) and returns UnicodeString which is default for String in C++Builder 10+ versions.

Here is the ExtractFileDir C++ syntax

Here is a simple C++ example of how to use the ExtractFileDir method

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

the output dir string will be “D:\MainFolder\SubFolder”.

Here is a simple full C++ Builder VCL Example of using the ExtractFileDir method

The ShowMessage() command will extract and display drive and folders from this string as below,

We can easily use this method with the 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.
Why not check out the latest C++ Builder Community Edition for free. For professional user’s download and try the C++ Builder trial version free for 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.

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

How To Generate A Random File Name on Windows

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

How To Include Or Exclude Trailing Backslashes On Windows

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

How To Check Characters of File Names and Paths on Windows

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

How to use SameFileName method on Windows

en_USEnglish