Returns the file attributes of FileName. UnitSysUtilsCategoryfile management routinesDelphi syntax:function FileGetAttr(const FileName: string): Integer;C++ syntax:extern PACKAGE int __fastcall FileGetAttr(const AnsiString FileName);DescriptionFileGetAttr returns the attributes of the file as a string of bits. This value is the same as the Attr field of a TSearchRec type. Check for individual attributes with code such as the following:Delphi example:Attrs := FileGetAttr('MyFile.sys');if Attrs and faHidden <> 0 then FileSetAttr('MyFile.sys', Attrs ?faHidden);Delphi example:int Attrs = FileGetAttr("MyFile.sys");if (Attrs & faHidden) FileSetAttr("MyFile.sys", Attrs & !faHidden);A return value of -1 indicates that an error occurred.Note: See TSearchRec for a description of the individual attribute constants. Note: FileGetAttr is Windows-only.
The following code reads a file's attributes into a set variable, sets the check boxes in a file-attribute dialog box to represent the current attributes, then executes the dialog box. If the user changes and accepts any dialog box settings, the code sets the file attributes to match the changed settings:procedure TFMForm.Properties1Click(Sender: TObject); var Attributes, NewAttributes: Word; begin with FileAttrForm do begin FileDirName.Caption := FileList.Items[FileList.ItemIndex]; { set box caption } PathName.Caption := FileList.Directory; { show directory name } ChangeDate.Caption := DateTimeToStr(FileDateToDateTime(FileAge(FileList.FileName))); Attributes := FileGetAttr(FileDirName.Caption); { read file attributes } ReadOnly.Checked := (Attributes and SysUtils.faReadOnly) = faReadOnly; Archive.Checked := (Attributes and faArchive) = faArchive; System.Checked := (Attributes and faSysFile) = faSysFile; Hidden.Checked := (Attributes and faHidden) = faHidden; if ShowModal <> id_Cancel then { execute dialog box } begin NewAttributes := Attributes; { start with original attributes } if ReadOnly.Checked then NewAttributes := NewAttributes or SysUtils.faReadOnly else NewAttributes := NewAttributes and not SysUtils.faReadOnly; if Archive.Checked then NewAttributes := NewAttributes or faArchive else NewAttributes := NewAttributes and not faArchive; if System.Checked then NewAttributes := NewAttributes or faSysFile else NewAttributes := NewAttributes and not faSysFile; if Hidden.Checked then NewAttributes := NewAttributes or faHidden else NewAttributes := NewAttributes and not faHidden; if NewAttributes <> Attributes then { if anything changed... } FileSetAttr(FileDirName.Caption, NewAttributes); { ...write the new values } end; end; end;
FileSetAttr('MyFile.sys', Attrs ?faHidden);Delphi example:int Attrs = FileGetAttr("MyFile.sys");if (Attrs & faHidden)
FileSetAttr("MyFile.sys", Attrs & !faHidden);A return value of -1 indicates that an error occurred.Note: See TSearchRec for a description of the individual attribute constants.
Note: FileGetAttr is Windows-only.
var
Attributes, NewAttributes: Word;
begin
with FileAttrForm do
begin
FileDirName.Caption := FileList.Items[FileList.ItemIndex];
{ set box caption }
PathName.Caption := FileList.Directory;
{ show directory name }
ChangeDate.Caption :=
DateTimeToStr(FileDateToDateTime(FileAge(FileList.FileName)));
Attributes := FileGetAttr(FileDirName.Caption);
{ read file attributes }
ReadOnly.Checked := (Attributes and SysUtils.faReadOnly) = faReadOnly;
Archive.Checked := (Attributes and faArchive) = faArchive;
System.Checked := (Attributes and faSysFile) = faSysFile;
Hidden.Checked := (Attributes and faHidden) = faHidden;
if ShowModal <> id_Cancel then { execute dialog box }
begin
NewAttributes := Attributes;
{ start with original attributes }
if ReadOnly.Checked then
NewAttributes := NewAttributes or SysUtils.faReadOnly
else
NewAttributes := NewAttributes and not SysUtils.faReadOnly;
if Archive.Checked then
NewAttributes := NewAttributes or faArchive
else
NewAttributes := NewAttributes and not faArchive;
if System.Checked then
NewAttributes := NewAttributes or faSysFile
else
NewAttributes := NewAttributes and not faSysFile;
if Hidden.Checked then
NewAttributes := NewAttributes or faHidden
else
NewAttributes := NewAttributes and not faHidden;
if NewAttributes <> Attributes then { if anything changed... }
FileSetAttr(FileDirName.Caption, NewAttributes);
{ ...write the new values }
end;
end;
end;