DelphiBasics
FileSetAttr
Function
Sets the attributes of a file SysUtils unit
 function FileSetAttr ( const FileName : string; Attributes : Integer; ) : Integer;
Description
The FileSetAttr function sets the attributes of the specified FileName.
 
The Attributes integer must be set to a combination of none, some or all of the following independent bit settings :
 
faReadOnly  : 1 : Read-only files
faHidden  : 2 : Hidden files
faSysFile  : 4 : System files
faVolumeID  : 8 : Volume ID files
faDirectory  : 16 : Directory files
faArchive  : 32 : Archive files
faSymLink  : 64 : Symbolic link

 
The returned Integer value is zero if the set was successful, otherwise it contains an error code.
Notes
This function is Operating System dependent. For example, Archive means nothing on Linux.

Important : During testing, the author always received a non-zero return code, even though the appropriate bits were set OK.
Related commands
FileGetAttr Gets 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
 Download this web site as a Windows program.

Development Boards at very low prices with free postage.


 
Example code : Create a text file, make is read only and hidden and display its attributes
var
  fileName : string;
  myFile   : TextFile;

  attrs    : Integer;

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

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

  // Close the file
  CloseFile(myFile);

  // Make the file read only and system
  if FileSetAttr(fileName, faReadOnly or faSysFile) > 0
  then ShowMessage('File made into a read only system file')
  else ShowMessage('File attribute change failed');

  // 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;
Show full unit code
   File made into a read only system file
   File is read only
   File is not hidden
   File is a system file
   File is not a Volume ID
   File is not a directory
   File is not archived
   File is not a symbolic link
 
 
Delphi Programming Neil Moffatt 2002 - 2014. All rights reserved.  |  Contact the author  |  Home Page