Site icon Learn C++

Extract A File Path For C++ App Development On Windows

C++ Builder is the easiest and fastest C and C++ IDE for everything from simple right through to professional applications development on 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.

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

ExtractFilePath Method

The ExtractFilePath method (System::SysUtils::ExtractFileName) is a Path Manipulation Routine that returns the drive and directory portions of a file name. The resulting string is the leftmost characters of FileName, up to and including the colon or backslash that separates the path information from the name and extension. The string is empty if FileName contains no drive and directory parts. Note that this function works for multi-byte character systems (MBCS).

Syntax:

[crayon-6633d550ad3de532347549/]

Simple Example:

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

[crayon-6633d550ad3e8354128341/]

the output file name string will be “D:\MainFolder\SubFolder\” including dot character. Note that, in comparison, ExtractFileDir() method returns “D:\MainFolder\SubFolder” without the last ‘\’ character while this method returns “D:\MainFolder\SubFolder\” . This method is very useful when adding new filenames to this path string.

Here is the simple full C++ Builder VCL Example,

[crayon-6633d550ad3ea889813090/]

Here ShowMessage() command will extract and display path from this string as “D:\MainFolder\SubFolder\” below,

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,

[crayon-6633d550ad3ec465251452/]

This will show the path of a file that you select by the OpenDialog component.

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.

Exit mobile version