DelphiBasics
FileGetAttr
Function
Gets the attributes of a file SysUtils unit
 function FileGetAttr ( const FileName : string; ) : Integer;
Description
The FileGetAttr function returns attributes of the specified FileName.
 
The returned Integer value will be a bitwise combination of the following independent attributes :
 
faReadOnly  : Read-only files
faHidden  : Hidden files
faSysFile  : System files
faVolumeID  : Volume ID files
faDirectory  : Directory files
faArchive  : Archive files
faSymLink  : Symbolic link
Notes
This function is Operating System dependent. For example, Archive means nothing on Linux.
Related commands
FileSetAttr Sets the attributes of a file
FileAge Get the last modified date/time of a file without opening it
FileSetDate Set the last modified date and time of a file
FileExists Returns true if the given file exists
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 Download this web site as a Windows program.




 
Example code : Create a text file and display its attributes
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the FileGetAttr command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  fileName : string;
  myFile   : TextFile;

  attrs    : Integer;

begin
  // Try to open a text file for writing to
  fileName := 'Test.txt';
  AssignFile(myFile, fileName);
  ReWrite(myFile);

  // Write to the file
  Write(myFile, 'Hello World');

  // Close the file
  CloseFile(myFile);

  // Get the file attributes
  attrs := FileGetAttr(fileName);

  // Display these attributes
  if attrs and faReadOnly > 0
  then ShowMessage('File is read only')
  else ShowMessage('File is not read only');

  if attrs and faHidden > 0
  then ShowMessage('File is hidden')
  else ShowMessage('File is not hidden');

  if attrs and faSysFile > 0
  then ShowMessage('File is a system file')
  else ShowMessage('File is not a system file');

  if attrs and faVolumeID > 0
  then ShowMessage('File is a volume ID')
  else ShowMessage('File is not a volume ID');

  if attrs and faDirectory > 0
  then ShowMessage('File is a directory')
  else ShowMessage('File is not a directory');

  if attrs and faArchive > 0
  then ShowMessage('File is archived')
  else ShowMessage('File is not archived');

  if attrs and faSymLink > 0
  then ShowMessage('File is a symbolic link')
  else ShowMessage('File is not a symbolic link');
end;
 
end.
Hide full unit code
   File is not read only
   File is not hidden
   File is not a system file
   File is not a Volume ID
   File is not a directory
   File is archived
   File is not a symbolic link
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page