C++Builder This Is How To Change A File Path In A String On Windows

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
This Is How To Change A File Path In A String On Windows
By Yilmaz Yoru December 29, 2021

C++ Builder is the easiest and fastest C and Для просмотра ссылки Войди или Зарегистрируйся 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 allow developers to edit, extract, get and set drive name, directory name, file name, and file extensions. These methods are combined in the Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся 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 use ChangeFilePath to change path of a file from given a file path String on Windows.

What is the ChangeFilePath method?​

Для просмотра ссылки Войди или Зарегистрируйся (System::SysUtils::ChangeFilePath) is a Path Manipulation Routine that changes the path of a file name. ChangeFilePath replaces the drive and directory parts of the passed FileName with the passed Path value. If Path is not blank, it must specify a drive, terminated by a colon, and/or a directory, terminated by a backslash.

Note that ChangeFilePath does not rename the actual file, it just creates a new file name string. This function works with multi-byte character sets (MBCS).

Syntax:
C++:
System::UnicodeString __fastcall ChangeFilePath(const System::UnicodeString FileName, const System::UnicodeString Path)/* overload */;
Simple Example:

We should use L”” literal for the UnicodeStrings and we can separate drives and folders in a path string with “\\” to define single \ and we can get new file name with path change from a given file path string as below,
C++:
String newname = ChangeFilePath( L"D:\\MainFolder\\SubFolder\\myimage.jpg", L"E:\\");
 
ShowMessage(newname);
This will change newname to “E:\myimage.jpg”. Now you can copy this file to this new path.
Note that again this ChangeFilePath method does not rename the actual file,

Is there an example of how to use the ChangeFilePath method to change a file path?​

Here is the simple full 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)
{
 String str = L"D:\\MainFolder\\SubFolder\\myimage.jpg";
 
 String newname = ChangeFilePath( str, L"E:\\");
 
 ShowMessage( newname );
 
 // Now we have new name with new path we can ask to move or move/copy this file
}
We can use this method with file name strings of any components. For example we can use this with OpneDialog. We can show the text of selected file with a extension change as below,
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)
{
 if( OpenDialog1->Execute() )
 {
 String newname = ChangeFilePath( OpenDialog1->FileName, L"E:\\");
 
 ShowMessage( newname );
 
 // Now we have new name with new path we can ask to move or move/copy this file
 }
}
In this example, user selects a file by using OpenDialog file browser and it generates a new file name string with a given path.