DirectoryOperations (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the use of methods in System.IOUtils.TDirectory class to create, delete, copy a directory, or to check if the specified directory is empty.

Code

procedure TForm1.btnCopyClick(Sender: TObject);
begin
  try
    { Copy directory from source path to destination path }
    TDirectory.Copy(edSourcePath.Text, edDestinationPath.Text);
  except
    { Catch the possible exceptions }
    MessageDlg('Incorrect source path or destination path', mtError, [mbOK], 0);
  end;
end;

procedure TForm1.btnCreateClick(Sender: TObject);
begin
  try
    { Create directory to specified path }
    TDirectory.CreateDirectory(edSourcePath.Text);
  except
    { Catch the possible exceptions }
    MessageDlg('Incorrect path', mtError, [mbOK], 0);
  end;
end;

procedure TForm1.btnDeleteClick(Sender: TObject);
var
  IsRecursive: Boolean;
begin
  try
    { Check if the deletion method is recursive }
    IsRecursive := cbIsRecursive.Checked;
    { Delete directory from specified path }
    TDirectory.Delete(edSourcePath.Text, IsRecursive);
  except
    { Catch the possible exceptions }
    MessageDlg('Incorrect path', mtError, [mbOK], 0);
  end;
end;

procedure TForm1.btnIsEmptyClick(Sender: TObject);
begin
  try
    { Check if the specified directory is empty }
    if TDirectory.IsEmpty(edSourcePath.Text) then
      MessageDlg('The specified directory is empty', mtInformation, [mbOK], 0)
    else
      MessageDlg('The specified directory is not empty', mtInformation, [mbOK], 0);
  except
    { Catch the possible exceptions }
    MessageDlg('Incorrect path', mtError, [mbOK], 0);
  end;
end;

Uses