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 ExtractShortPathName Method to extract a Short Path Name from a file path String on Windows.
ExtractShortPathName Method
ExtractShortPathName method (System::SysUtils::ExtractShortPathName) is a Path Manipulation Routine that converts a 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 “C:\Program Files\MyCompany\MyApp\MyApp.exe”
– to this “C:\Progra~1\MyComp~1\MyApp\MyApp.exe” string.
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.
Syntax:
1 2 3 |
System::UnicodeString __fastcall ExtractShortPathName(const System::UnicodeString FileName); // overload |
Simple Example:
We can separate drives and folders in a path string with “\\” to define single \ and we can extract short path name from a given file path string as below,
1 2 3 |
String shortpath = ExtractShortPathName( L"C:\\Program Files\\MyCompany\\MyApp\\MyApp.exe" ); |
Here file path string “C:\Program Files\MyCompany\MyApp\MyApp.exe”
will be extracted as “C:\Progra~1\MyComp~1\MyApp\MyApp.exe“. If it is doesn’t exist it will return empty string.
This method is very useful when you want to display paths of files in a short view. Here is the simple full C++ Builder VCL Example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#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 str = L"C:\\Program Files\\MyCompany\\MyApp\\MyApp.exe"; String shortpath = ExtractShortPathName( str ); ShowMessage( L"Short Path: " + shortpath); } |
Here ShowMessage() command will extract and display short path from this string as “C:\Progra~1\MyComp~1\MyApp\MyApp.exe”. Note that this path and file should be exist otherwise it returns empty string.
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 25 26 |
#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) { String str = L"D:\\MainFolder\\SubFolder\\myimage.jpg"; OpenDialog1->Execute(); String shortpath = ExtractShortPathName( OpenDialog1->FileName ); ShowMessage( L"Short Path: " + shortpath); } |
This will show the short path name in 8.3 format. This method can be used if you don’t want to display full long path.
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.
You can download and use the latest C++ Builder Community Edition free. You can download and try the C++ Builder trial version 30 days.