PathOperations (Delphi)
From RAD Studio XE2 Code Examples
Language:
Description
This examples demonstrates the use of methods in TPath class to do operations with files and directories.
Code
procedure TForm1.btnChangeExtentionClick(Sender: TObject); begin { Change the extention of the given file or directory with the specified extention } try edPath.Text := TPath.ChangeExtension(edPath.Text, edExtention.Text); except MessageDlg('Incorrect path or extention', mtError, [mbOK], 0); end; end; procedure TForm1.btnDriveExistsClick(Sender: TObject); begin { Check if drive exists } if TPath.DriveExists(edPath.Text) then MessageDlg('Drive exists!', mtInformation, [mbOK], 0) else MessageDlg('Not drive found to specified path!', mtInformation, [mbOK], 0); end; procedure TForm1.btnGetDirectoryNameClick(Sender: TObject); var LDirectoryName: String; begin { Extract the directory name from specified parh } try LDirectoryName := TPath.GetDirectoryName(edPath.Text); if LDirectoryName = '' then MessageDlg('No directory found at specified path', mtInformation, [mbOK], 0) else MessageDlg('The directory name is: ' + LDirectoryName, mtInformation, [mbOK], 0); except MessageDlg('Incorrect path!', mtError, [mbOK], 0); end; end; procedure TForm1.btnGetExtentionClick(Sender: TObject); var LExtention: String; begin { Extract the extention from specified file name } try LExtention := TPath.GetExtension(edPath.Text); if LExtention = '' then MessageDlg('The file name has no extention', mtInformation, [mbOK], 0) else MessageDlg('The extention is: ' + LExtention, mtInformation, [mbOK], 0); except MessageDlg('Incorrect file name!', mtError, [mbOK], 0); end; end; procedure TForm1.btnGetFileNameClick(Sender: TObject); var LFileName: String; begin { Extract the file name from specified parh } try LFileName := TPath.GetFileName(edPath.Text); if LFileName = '' then MessageDlg('No file found at specified path', mtInformation, [mbOK], 0) else MessageDlg('The file name is: ' + LFileName, mtInformation, [mbOK], 0); except MessageDlg('Incorrect path!', mtError, [mbOK], 0); end; end; procedure TForm1.btnHasExtentionClick(Sender: TObject); begin { Check if file has an extention } if TPath.HasExtension(edPath.Text) then MessageDlg('The file has extention!', mtInformation, [mbOK], 0) else MessageDlg('Incorrect path!', mtInformation, [mbOK], 0); end;
Uses
- System.IOUtils.TPath.ChangeExtension ( fr | de | ja )
- System.IOUtils.TPath.DriveExists ( fr | de | ja )
- System.IOUtils.TPath.GetDirectoryName ( fr | de | ja )
- System.IOUtils.TPath.GetExtension ( fr | de | ja )
- System.IOUtils.TPath.GetFileName ( fr | de | ja )
- System.IOUtils.TPath.HasExtension ( fr | de | ja )