RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.ChangeFileExt Function

Changes the extension of a file name.

Pascal
function ChangeFileExt(const FileName: string; const Extension: string): string;
C++
AnsiString ChangeFileExt(const AnsiString FileName, const AnsiString Extension);

SysUtils

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: This function works with multi-byte character sets (MBCS).
 

C++ Examples: 

 

/*
This example converts a specified icon to a bitmap. To run 
this example, add an image, a button, and an Open dialog to 
a form. Name the button ConvertIcon2Bitmap, and add the 
following code as its OnClick event handler.
*/
void __fastcall TForm1::ConvertIcon2BitmapClick(TObject *Sender)
{
  OpenDialog1->DefaultExt = ".ICO";
  OpenDialog1->Filter = "icons (*.ico)|*.ICO";
  OpenDialog1->Options << ofOverwritePrompt << ofFileMustExist << ofHideReadOnly;
  if (OpenDialog1->Execute())
  {
    TIcon *pi = new TIcon();
    try
    {
      AnsiString as;
      pi->LoadFromFile(OpenDialog1->FileName);
      as = ChangeFileExt(OpenDialog1->FileName,".BMP");
      Image1->Width = pi->Width;
      Image1->Height = pi->Height;
      Image1->Canvas->Draw(0,0,pi);
      Image1->Picture->SaveToFile(as);
      ShowMessage(OpenDialog1->FileName + " Saved to " + as);
    }
    __finally
    {
      delete pi;
    }
  }
}

 

Delphi Examples: 

{
This example converts a specified icon to a bitmap. To run 
this example, add an image, a button, and an Open dialog to 
a form. Name the button ConvertIcon2Bitmap, and add the 
following code as its OnClick event handler.
} 
procedure TForm1.ConvertIcon2BitmapClick(Sender: TObject);
var 
  s : string;
  Icon: TIcon;
begin
  OpenDialog1.DefaultExt := '.ICO';
  OpenDialog1.Filter := 'icons (*.ico)|*.ICO';
  OpenDialog1.Options := [ofOverwritePrompt, ofFileMustExist, ofHideReadOnly ];
  if OpenDialog1.Execute then
  begin
    Icon := TIcon.Create;
    try
      Icon.Loadfromfile(OpenDialog1.FileName);
      s:= ChangeFileExt(OpenDialog1.FileName,'.BMP');
      Image1.Width := Icon.Width;
      Image1.Height := Icon.Height;
      Image1.Canvas.Draw(0,0,Icon);
      Image1.Picture.SaveToFile(s);
      ShowMessage(OpenDialog1.FileName + ' Saved to ' + s);
    finally
      Icon.Free;
    end;
  end;
end; 

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!