C++Builder How To Change A File Extension On Windows Using C+

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Change A File Extension On Windows Using C+
By Yilmaz Yoru August 30, 2021

C++ Builder has specific Для просмотра ссылки Войди или Зарегистрируйся that allows user to edit, extract, get and set drive name, directory name, file name, file extensions, .. etc and these methods are combined in Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся 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 ChangeFileExt to change extension of a file from given a file path String on Windows.

Use the ChangeFileExt method​

Для просмотра ссылки Войди или Зарегистрируйся (System::SysUtils::ChangeFileExt) is a Path Manipulation Routine that creates a new file name string changes the extension of a file name. ChangeFileExt takes the file name passed in FileName and changes the extension of the file name to the extension passed in Extension. Extension specifies the new extension, including the initial dot character.

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

Here is the ChangeFileExt method’s syntax​

C++:
System::UnicodeString __fastcall ChangeFileExt(const System::UnicodeString FileName, const System::UnicodeString Extension); // overload

A simple example of how to use the ChangeFileExt method in C++​

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 extension change from a given file path string as below,
C++:
String newname = ChangeFileExt( L"D:\\MainFolder\\SubFolder\\myimage.jpg", L".gif");
 
ShowMessage(newname);
Note that again this ChangeFileExt method does not rename the actual file.

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 = ChangeFileExt( str, L".gif");
 
 ShowMessage( newname );
 
 // Now we have new name we can ask or change to this name
}
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 = ChangeFileExt( OpenDialog1->FileName, L".gif");
 
 ShowMessage( newname );
 }
}
In this example, user selects a file by using OpenDialog file browser and it generates a new file name string with a given extension.