In this post, you’ll get answers to these questions:
- What is the difference between the Dir, Directory and the Folder terms?
- What is a Drive? What is a Directory?
- What is a Path?
- What does Full Path mean?
- What does Absolute Path mean?
- What is the Current Path?
- What is a Short Path?
- What does Relative Path mean?
By learning how to understand file and folder paths in programming, it will help you to easily build C++ applications with the use of a C++ IDE.
Table of Contents
What do we mean by “Drive” in programming?
The Drive is the partition of a Disc that is format in the appropriate file format of an operating system . Generally it is shown by a drive letter with columns or a drive name with columns , (i.e.” C:” or “MyDrive:”) For example:
1 2 3 |
D: |
What does Dir, Directory or Folder mean?
The Directory term is the same as the Folder term that defines name of file container group in a file system hierarchy. Dir is the shorten of Directory term generally used in commands and in some path methods. For example;
1 2 3 |
Projects |
What do programmers mean when they say “Path”?
Path is a list of directory names with slash-separated and followed by either a directory name or a file name. All these examples below are Path,
1 2 3 |
D:\ |
1 2 3 |
D:\Projects |
1 2 3 |
D:\Projects\MyProject |
1 2 3 |
D:\Projects\MyProject\Win32 |
1 2 3 |
D:\Projects\MyProject\Win32\Debug |
1 2 3 |
Win32\Debug |
1 2 3 |
..\Win32\Debug |
1 2 3 |
D:\Projects\MyProject\Win32\Debug\MyApp.exe |
In these examples, the last Path example points a file while the others point a folder location.
How do I return the full path of a file?
Full Path is also known as Absolute Path, begins with a drive letter followed by a colon, such as D: For example this is a full path file name.
1 2 3 |
D:\Projects\MyProject\Win32\Debug\MyApp.exe |
So full path should start with a drive letter or name.
What do programmers mean by “Current Path” or “Current Directory”?
Current Path is the path that is normally 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.
1 2 3 |
D:\Projects\MyProject\Win32\Debug\ |
Current Directory is the Directory where we are in, in this example it is “Debug”. In some cases Current Directory is shown in Full Path as same in Current Path.
What is a “Relative Path”?
Relative Path is the path that is relative to a current directory. 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 at the “D:\Projects\MyProject\Win32\Debug\MyApp.exe” application, Thus Relative Path of this folder:
1 2 3 |
D:\Projects\MyProject\Win32\Debug\Images\ |
is equal to:
1 2 3 |
Images |
In C++ Builder, we can use RelativePath Method to get Relative Path of a Path String.
What do programmers mean by “Short Path”?
Short Path term is used to define old short 8.3 form of windows operating system.
ExtractShortPathName method to convert a path file name to the short 8.3 form. In other terms this method converts the file name, FileName, to the short 8.3 form. For example;
It return this Full Path
1 2 3 |
C:\Program Files\MyCompany\MyApp\MyApp.exe |
To this Short Path string,
1 2 3 |
C:\Progra~1\MyComp~1\MyApp\MyApp.exe |
ExtractShortPathName returns an empty string if the file or directory does not exist and ExtractShortPathName is only available on Windows. The first parameter, FileName, can be a full path name.
What is a UNC Path?
UNC Path (Uniform (Universal or Unified) Naming Convention Path) is a syntax for accessing folders and files on a network of computers. For example,
1 2 3 |
\\<computer name>\<shared directory>\ |
C++ Builder has some UNC specific Path Manipulation Routines that allows to use UNC formats like isUNCPath, IsUNCRooted, ExpandUNCFileName Methods.
What is a URL Path?
URL Path (Uniform Resource Locator Path) is used to locate the address of any document or folder on the Internet. For example,
1 2 3 |
https://www.embarcadero.com/products/cbuilder |
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 Path Manipulation Routines which allow a user to edit, extract, get and set drive name, directory name, file name, file extensions. These methods are combined in Vcl.FileCtrl, System.IOUtils, System.SysUtils libraries. These 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 if they exist or not in that given path. And we can get file name from a given path string.
Is there a C++ Builder Example of using the Path methods?
Here is the full C++ example about the some of the Path Methods that we used in programming,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <vcl.h> #include <IOUtils.hpp> #pragma hdrstop #include "Get_Methods_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { String dir, path, file, ext; // GetDirectory Example 1 dir = TPath::GetDirectoryName( L"D:\\MainFolder\\SubFolder\\MyFile.jpg"); Memo1->Lines->Add( "GetDirectory 1: " + dir ); // ShowMessage(dir); // GetDirectory Example 2 dir = TPath::GetDirectoryName( L"\\MainFolder\\SubFolder\\MyFile.jpg"); Memo1->Lines->Add( "GetDirectory 2: " + dir ); // ShowMessage(dir); // GetDirectory Example 3 dir = TPath::GetDirectoryName( L"MainFolder\\SubFolder\\MyFile.jpg"); Memo1->Lines->Add( "GetDirectory 3: " + dir ); // ShowMessage(dir); // GetExtension Example ext = TPath::GetExtension( L"D:\\MainFolder\\SubFolder\\MyFile.jpg"); Memo1->Lines->Add( "GetExtension: " + ext ); // ShowMessage(ext); // GetFileName Example file = TPath::GetFileName( L"D:\\MainFolder\\SubFolder\\MyFile.jpg"); Memo1->Lines->Add( "FileName: " + file ); // ShowMessage(file); // GetFileNameWithoutExtension Example file = TPath::GetFileNameWithoutExtension( L"D:\\MainFolder\\SubFolder\\MyFile.jpg"); Memo1->Lines->Add( "GetFileNameWithoutExtension: " + file ); // ShowMessage(file); path = GetCurrentDir(); Memo1->Lines->Add( "GetCurrentDirectory: " + path); // ShowMessage(path); path = TPath::GetFullPath( L"." ); Memo1->Lines->Add( "GetFullPath: " + path); // ShowMessage(path); path = TPath::GetFullPath( L"..\\Test"); Memo1->Lines->Add( "GetFullPath ..\\Test: " + path); // ShowMessage(path); } |
These above and all other Path Methods are listed in Path Manipulation Routines. You can use them in your applications.
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.