Title: Changing attributes of files
Question: How can I get the attributes of the files?
Answer:
To read a file's attributes, pass the filename to the FileGetAttr function, which returns the file attributes of a file.For example, add a TButton and TLabel components to the form and write the following code to the OnClick event of button.
var
attr:Integer;
s:string;
begin
attr:=FileGetAttr('c:\Autoexec.bat');
if(attr and faHidden)0 then s:='Hidden';
if(attr and faReadOnly)0 then s:=s+'Read-Only';
if(attr and faSysFile)0 then s:=s+'System';
if(attr and faArchive)0 then s:=s+'Archive';
Label1.Caption:=s;
---------------------------
To set a file's attributes, pass the name of the file and the attributes you want to the FileSetAttr function.Each attribute has a mnemonic name defined in the SysUtils unit. For example, to set a file's system attribute, you would do the following:
Attributes := Attributes or faSystem;
You can also set or clear several attributes at once. For example, the clear both the read-only file and hidden attributes:
Attributes := Attributes and not (faReadOnly or faHidden);
---------------------------
Furthermore,To change file's attribute, you can use the following return values.The routine can be simplified.
+----------------------------------+
| Return Value | Attribute of File |
+----------------------------------+
| 128 | Normal |
| 1 | Read Only |
| 2 | Hidden |
| 4 | System |
| 32 | Archive |
+--------------+-------------------+
Sample call: We use a code like the following:
FileSetAttr('C:\Autoexec.bat',2);{Hidden}
FileSetAttr('C:\Autoexec.bat',3);{Hidden and Read-Only.
FileGetAttr returns 3 value}