How To Understand File And Folder Paths In Programming
By Yilmaz Yoru September 30, 2021
What is 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?
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:
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;
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,
| D:\Projects\MyProject\Win32 |
| D:\Projects\MyProject\Win32\Debug |
| 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.
| 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.
| 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:
| D:\Projects\MyProject\Win32\Debug\Images\ |
is equal to:
In C++ Builder, we can use
Для просмотра ссылки Войди или Зарегистрируйся 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.
Для просмотра ссылки Войди или Зарегистрируйся 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
| C:\Program Files\MyCompany\MyApp\MyApp.exe |
To this
Short Path string,
| 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,
| \\<computer name>\<shared directory>\ |
C++ Builder has some UNC specific
Для просмотра ссылки Войди или Зарегистрируйся that allows to use UNC formats like
Для просмотра ссылки Войди или Зарегистрируйся,
Для просмотра ссылки Войди или Зарегистрируйся,
Для просмотра ссылки Войди или Зарегистрируйся 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,
C++ Builder has a lot of specific methods in its
Для просмотра ссылки Войди или Зарегистрируйся and
Для просмотра ссылки Войди или Зарегистрируйся library that are included in VCL and FMX libraries. Some of these are grouped as
Для просмотра ссылки Войди или Зарегистрируйся which allow a user to edit, extract, get and set drive name, directory name, file name, file extensions. These methods are combined in
Для просмотра ссылки Войди или Зарегистрируйся,
Для просмотра ссылки Войди или Зарегистрируйся,
Для просмотра ссылки Войди или Зарегистрируйся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,
C++:
#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);
}