Returns the drive and directory portions of a file name.
function ExtractFilePath(const FileName: string): string;
AnsiString ExtractFilePath(const AnsiString FileName);
SysUtils
The resulting string is the leftmost characters of FileName, up to and including the colon or backslash that separates the path information from the name and extension. The resulting string is empty if FileName contains no drive and directory parts.
This function works for multi-byte character systems (MBCS).
C++ Examples:
/* This example copies a specified old file to a new file. Add a TSaveDialog to the form. Also add two TEdits, two TLabels and a TButton with the OnClick event named Save1Click. Files can be saved to and from the app directory by using relative paths. */ void __fastcall TForm1::Save1Click(TObject *Sender) { AnsiString NewFileName = ExtractFilePath(Application->ExeName) + ExtractFileName(Edit1->Text); AnsiString OldFileName = ExtractFilePath(Application->ExeName) + ExtractFileName(Edit2->Text); AnsiString Msg = Format("Copy %s to %s", ARRAYOFCONST((OldFileName, NewFileName))); if (MessageDlg(Msg, mtCustom, TMsgDlgButtons() << mbOK << mbCancel, 0) == mrOk) { TFileStream *OldFile = new TFileStream(OldFileName, fmOpenRead); try { TFileStream *NewFile = new TFileStream(NewFileName, fmCreate); try { NewFile->CopyFrom(OldFile, OldFile->Size); } __finally { FreeAndNil(NewFile); } } __finally { FreeAndNil(OldFile); } } } void __fastcall TForm1::FormCreate(TObject *Sender) { Label3->Caption = "Current directory: " + ExtractFilePath(Application->ExeName); }
/* The following code prompts for confirmation before deleting a file. Create a file and then click on the button to select the file with a TOpenDialog. Click Yes to delete it. */ void __fastcall TForm1::Button1Click(TObject *Sender) { OpenDialog1->Title = "Delete File"; if (OpenDialog1->Execute()) { if (FileExists(OpenDialog1->FileName)) { if (MessageDlg( ("Do you really want to delete " + ExtractFileName(OpenDialog1->FileName) + "?"), mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0, mbNo) == mrYes) DeleteFile(OpenDialog1->FileName); } else MessageDlg(("File " + ExtractFileName(OpenDialog1->FileName) + " does not exist."), mtConfirmation, TMsgDlgButtons() << mbOK, 0); } }
Delphi Examples:
{ This example copies a specified old file to a new file. Add a TSaveDialog to the form. Also add two TEdits, two TLabels and a TButton with the OnClick event named Save1Click. Files can be saved to and from the app directory by using relative paths. } procedure TForm1.FormCreate(Sender: TObject); begin Label3.Caption:= 'Current directory: ' + ExtractFilePath(Application.ExeName); end; procedure TForm1.Save1Click(Sender: TObject); var NewFileName, OldFileName: string; Msg: string; NewFile: TFileStream; OldFile: TFileStream; begin NewFileName := ExtractFilePath(Application.ExeName) + ExtractFileName(Edit1.Text); OldFileName := ExtractFilePath(Application.ExeName) + ExtractFileName(Edit2.Text); Msg := Format('Copy %s to %s?', [OldFileName, NewFileName]); if MessageDlg(Msg, mtCustom, mbOKCancel, 0) = mrOK then begin OldFile := TFileStream.Create( OldFileName, fmOpenRead or fmShareDenyWrite); try NewFile := TFileStream.Create( NewFileName, fmCreate or fmShareDenyRead); try NewFile.CopyFrom(OldFile, OldFile.Size); finally FreeAndNil(NewFile); end; finally FreeAndNil(OldFile); end; end; end;
{ The following code prompts for confirmation before deleting a file. Create a file and then enter the path and file name in the textfields. Click the button to delete it. } procedure TForm1.Button1Click(Sender: TObject); var FileName: string; begin Filename:= Edit1.Text; if SysUtils.FileExists(FileName) then begin if MessageDlg(('Do you really want to delete ' + ExtractFileName(FileName) + '?'), mtConfirmation, [mbYes, mbNo], 0, mbNo) = IDYes then DeleteFile(FileName); end else MessageDlg(('File ' + ExtractFileName(FileName) + ' does not exist.'), mtConfirmation, [mbOK], 0); end; procedure TForm1.FormCreate(Sender: TObject); begin Edit2.Text:= ExtractFilePath(Application.ExeName); end;
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|