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.
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.FileCtrl, System.IOUtils, System.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 ExtractRelativePath Method to extract a Realtive Path from the two path Strings on Windows.
Table of Contents
What is the C++ ExtractRelativePath method?
ExtractRelativePath method (System::SysUtils::ExtractRelativePath) is a Path Manipulation Routine that returns a relative path name from given two path strings, relative to a specific base directory. Call to convert a fully qualified path name into a relative path name. The DestName parameter specifies the file name (including path) to be converted. BaseName is the fully qualified name of the base directory to which the returned path name should be relative. BaseName may or may not include a file name, but it must include the final path delimiter. If you send the function what should be a directory without the trailing backslash (c:\foo instead of c:\foo\), the function treats foo as a file and returns undesirable results.
ExtractRelativePath Syntax
1 2 3 |
System::UnicodeString __fastcall ExtractRelativePath(const System::UnicodeString BaseName, const System::UnicodeString DestName); // overload |
Simple ExtractRelativePath Example
We can separate drives and folders in a path string with “\\” to define single \ and we can extract relative path from two path strings as below,
1 2 3 |
String relpath = ExtractRelativePath( L"D:\\MainFolder\\" , L"D:\\MainFolder\\SubFolder\\myimage.jpg" ); |
the output file name string will be “SubFolder\myimage.jpg”. This method is very useful when comparing two path strings.
Here is a simple C++ Builder VCL example of extracting a relative file path
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { String base = L"D:\\MainFolder\\"; String dest = L"D:\\MainFolder\\SubFolder\\myimage.jpg"; String relpath = ExtractRelativePath( base , dest); ShowMessage( L"Relative Path: " + relpath); } |
Here ShowMessage() command will extract and display path from this string as “SubFolder\” below,
We can easily use this method with the FileName property of OpenDialog, SaveDialog components or with the String / UnicodeString properties of the other components. Here is the C++ Builder VCL Example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { OpenDialog1->Execute(); String relpath = ExtractFileDir( L"D:\\", OpenDialog1->FileName); ShowMessage( L"Relative Path: " + relpath); } |
This will show the relative path of a file in comparison with “D\\” with the FileName 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.
Why not download and try the free C++ Builder trial version?
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition