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

How To Get A Full Path From A String On Windows?

In this post, you’ll learn how to get a full path from a path string in C++ Software. What are the GetFullPath Method, its syntax, and examples of how to use it?

How to understand paths on Windows?

The full path is also known as the absolute path. It begins with a drive letter followed by a colon, such as D: For example, this is a full path file name.

The current path is the path that is normally file path of your application that you are running. We can change the current path on runtime, so every new folder or file operation may occur in that path.

The relative path is the path that is ‘relative’ to a current directory – by that we mean at the same level or a deeper level, but without having the full path expressed. We can use a dot (.) and a double-dot (..) to translate it as the current directory and the parent directory. Here single dot represents the current directory itself and double dots are used for parent directory.

Let’s assume our app is stored as “D:\Projects\MyProject\Win32\Debug\MyApp.exe“. The relative path of the folder:

is equal to:

Are there any other C++ Builder path methods?

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 a Path Manipulation Routines that allow the user to edit, extract, get and set drive name, directory name, file name, file extensions and so on. These methods are combined in Vcl.FileCtrlSystem.IOUtilsSystem.SysUtils libraries. These are all methods which 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 GetFullPath Method to get full path from a a path string file name on Windows.

What is the GetFullPath method?

GetFullPath (System::IOUtils::TPath:: GetFullPath) is a Path Manipulation Routine that returns the absolute path for a given path. The full path, also known as the absolute path, begins with a drive letter followed by a colon, such as D: For example this is a full path file name: D:\myfolder\anotherfolder\myapp.exe

GetFullPath returns the full, absolute path for a given relative path. If the given path is absolute, GetFullPath simply returns it; otherwise, GetFullPath uses the current working directory as a root for the given path. The following table lists the parameters expected by this method:

NameMeaning
PathThe relative path

Note: GetFullPath raises an exception if the given path contains invalid characters.

What is the Syntax of the GetFullPath Method ?

Here is the Syntax of GetFullPath() method,

Here the FileName is the file name from which the name and extension parts should be extracted.

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

Here is a simple example of how to use the GetFullPath Method

GetFileNameWithoutExtension Method returns a UnicodeString. With it we can get the file name without an extension from a path string as in example below,

Example:

Assume that you have an application in this path;

If we run this MyApp.exe and if we use GetFullPath Method as in example below,

the output of ShowMessage() will display this result path string as below,

Is there a full example of using the GetFullPath method?

Here is the full example to GetFullPath Method,

If we run this example in this Debug folder;

The output will be as follows:

as you see if we ask for L”.” it returns same full path of current directory that we run the application. You can copy in other folders and run your executable to see changes.

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++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?