Site icon Learn 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.

[crayon-6767091109809182684556/]

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.

[crayon-6767091109811419851937/]

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:

[crayon-6767091109813273394141/]

is equal to:

[crayon-6767091109814870571438/]

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,

[crayon-6767091109815801058852/]

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;

[crayon-6767091109817818512730/]

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

[crayon-6767091109819714333260/]

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

[crayon-676709110981a985277838/]

Is there a full example of using the GetFullPath method?

Here is the full example to GetFullPath Method,

[crayon-676709110981b235030305/]

If we run this example in this Debug folder;

[crayon-676709110981c375406741/]

The output will be as follows:

[crayon-676709110981d871036439/]

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.



Exit mobile version