FileAttributes (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This examples demonstrates the use of getting and setting the attributes to a file.

Code

procedure TForm1.btnBrowseClick(Sender: TObject);
var
  LFileAttributes: TFileAttributes;
begin
  try
  { Browse a file and read its attributes }
  if OpenDialog1.Execute then
    LFileAttributes := TFile.GetAttributes(OpenDialog1.FileName);

  cbReadOnly.Checked := (TFileAttribute.faReadOnly in LFileAttributes);
  cbHidden.Checked := (TFileAttribute.faHidden in LFileAttributes);
  cbSystem.Checked := (TFileAttribute.faSystem in LFileAttributes);
  cbDirectory.Checked := (TFileAttribute.faDirectory in LFileAttributes);
  cbArchive.Checked := (TFileAttribute.faArchive in LFileAttributes);
  cbDevice.Checked := (TFileAttribute.faDevice in LFileAttributes);
  cbNormal.Checked := (TFileAttribute.faNormal in LFileAttributes);
  cbTemporary.Checked := (TFileAttribute.faTemporary in LFileAttributes);
  cbSparseFile.Checked := (TFileAttribute.faSparseFile in LFileAttributes);
  cbReparsePoint.Checked := (TFileAttribute.faReparsePoint in LFileAttributes);
  cbCompressed.Checked := (TFileAttribute.faCompressed in LFileAttributes);
  cbOffline.Checked := (TFileAttribute.faOffline in LFileAttributes);
  cbNotContentIndexed.Checked := (TFileAttribute.faNotContentIndexed in LFileAttributes);
  cbEncrypted.Checked := (TFileAttribute.faEncrypted in LFileAttributes);

  except
    { Catch the possible exceptions }
    MessageDlg('No file selected', mtError, [mbOK], 0);
    Exit;
  end;
end;

procedure TForm1.btnSaveClick(Sender: TObject);
var
  LFileAttributes: TFileAttributes;
begin
  try
    { Check for attributes }
    if cbReadOnly.Checked then
      Include(LFileAttributes, TFileAttribute.faReadOnly);

    if cbHidden.Checked then
      Include(LFileAttributes, TFileAttribute.faHidden);

    if cbSystem.Checked then
      Include(LFileAttributes, TFileAttribute.faSystem);

    if cbArchive.Checked then
      Include(LFileAttributes, TFileAttribute.faArchive);

    if cbCompressed.Checked then
      Include(LFileAttributes, TFileAttribute.faCompressed);

    if cbOffline.Checked then
      Include(LFileAttributes, TFileAttribute.faOffline);

    { Save the attributes to the opened file }
    TFile.SetAttributes(OpenDialog1.FileName, LFileAttributes);

  except
    { Catch the possible exceptions }
    MessageDlg('No file selected', mtError, [mbOK], 0);
    Exit;
  end;
end;

Uses