C++Builder How To Extract The File Directory Name From A File Path In C++

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
How To Extract The File Directory Name From A File Path In C++
By Yilmaz Yoru August 19, 2021

Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся that allows users to edit, extract, get and set drive name, directory name, file name, and file extensions. These methods are combined in Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся libraries. The path manipulation methods are easy to use and simplify the process of getting or setting file path strings. These can be used with other component properties like FileName property of OpenDialog, SaveDialog components.

Let’s see how we can use ExtractFileDir Method to extract a File Directory Name from the File Path String on Windows.

ExtractFileDir Method​

The Для просмотра ссылки Войди или Зарегистрируйся method (System::SysUtils::ExtractFileDir) is a Path Manipulation Routine that extracts the drive and directory parts from the FileName. The resulting string is a directory name suitable for passing to the Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся, and Для просмотра ссылки Войди или Зарегистрируйся functions. This string is empty if FileName contains no drive and directory parts. Note that this function works for multi-byte character systems (MBCS) and returns UnicodeString which is default for String in C++Builder 10+ versions.

Here is the ExtractFileDir C++ syntax​

C++:
System::UnicodeString __fastcall ExtractFileDir(const System::UnicodeString FileName); // overload

Here is a simple C++ example of how to use the ExtractFileDir method​

We can separate drives and folders in a path string with “\\” to define single \ and we can extract directory name as below,
C++:
String dir = ExtractFileDir( L"D:\\MainFolder\\SubFolder\\myimage.jpg" );
the output dir string will be “D:\MainFolder\SubFolder”.

Here is a simple full C++ Builder VCL Example of using the ExtractFileDir method​

C++:
#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"D:\\MainFolder\\SubFolder\\myimage.jpg";
 String dir ;
 
 dir = ExtractFileDir( str );
 
 ShowMessage( L"Path: " + dir );
}
The ShowMessage() command will extract and display drive and folders from this string as below,
1629442802863.png
We can easily use this method with the OpenDialog, SaveDialog components or with the string properties of other components. Here is the C++ Builder VCL Example,
C++:
#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 dir = ExtractFileDir( OpenDialog1->FileName );
 
 ShowMessage( L"Path:" + dir);
}