We help IT Professionals succeed at work.

Setting Attribute to a file

Medium Priority
677 Views
Last Modified: 2013-11-23
H0W can i Change the Attribute of a file
example.

 from hidden to visible
 from Readonly to deletable or editable
 etc.
Comment
Watch Question

Author

Commented:
please help me

Commented:
Something likd this

var
     wAttrb: Word;
     sTheFile: string;

begin
     sTheFile := 'C:\aHiddenFile.txt';
     wAttrb := sTheFile
                FileSetAttr(sTheFile , wAttrb and not faHidden);

end;




faReadOnly     $00000001     Read-only files
faHidden     $00000002     Hidden files
faSysFile     $00000004     System files
faVolumeID     $00000008     Volume ID files
faDirectory     $00000010     Directory files
faArchive     $00000020     Archive files
faAnyFile     $0000003F     Any file



The Crazy One
From Delphi help and it should helps

function FileGetAttr(const FileName: string): Integer;
Description
FileGetAttr returns the attributes of the file as a string of bits. This value is the same as the Attr field of a TSearchRec record. Check for individual attributes with code such as the following:
Attrs := FileGetAttr('MyFile.sys');
if Attrs and faHidden <> 0 then
  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.

******************************
function FileSetAttr(const FileName: string; Attr: Integer): Integer;

Description

FileSetAttr sets the file attributes of the file given by FileName to the value given by Attr. The value of Attr is formed by combining the appropriate file attribute constants, as in the following:

FileSetAttr('MyFile.sys', faReadOnly or faSysFile);

FileSetAttr returns zero if the function was successful. Otherwise the return value is a Windows error code.

Note:     See TSearchRec for a description of the file attribute constants.


******************************
TSearchRec defines file information searched for by FindFirst or FindNext.

Unit

Sysutils

type
TSearchRec = record
          Time: Integer;
          Size: Integer;
          Attr: Integer;
          Name: TFileName;
          ExcludeAttr: Integer;
          FindHandle: THandle;
          FindData: TWin32FindData;
end;

Description

The TSearchRec type defines file information searched for by a FindFirst or FindNext function call. If a file is found, the fields of the TSearchRec type parameter are modified to specify the found file.

Attr represents the file attributes the file attributes of the file. Test Attr against the following attribute constants or values to determine if a file has a specific attribute:

Constant     Value     Description

faReadOnly     $00000001     Read-only files
faHidden     $00000002     Hidden files
faSysFile     $00000004     System files
faVolumeID     $00000008     Volume ID files
faDirectory     $00000010     Directory files
faArchive     $00000020     Archive files
faAnyFile     $0000003F     Any file

To test for an attribute, combine the value of the Attr field with the attribute constant with the and operator. If the file has that attribute, the result will be greater than 0. For example, if the found file is a hidden file, the following expression will evaluate to True: (SearchRec.Attr and faHidden > 0).

Time contains the time stamp of the file. This is a DOS date-and-time stamp. It can be converted to a TDateTime value using FileDateToDateTime.

Size contains the size of the file in bytes.

Name contains the DOS file name and extension.  

FindData contains additional information such as the file creation time, last access time, and both the long and short file names.

Commented:
Oops this

    wAttrb := sTheFile

Sould be

    wAttrb := FileGetAttr(sTheFile);

Commented:
you could use the API
SetFileAttributes(PChar(FileName), FILE_ATTRIBUTE_NORMAL);

this will get rid of all arributes

some of the attributes you can set are
FILE_ATTRIBUTE_ARCHIVE
FILE_ATTRIBUTE_HIDDEN
FILE_ATTRIBUTE_OFFLINE
FILE_ATTRIBUTE_READONLY
FILE_ATTRIBUTE_SYSTEM
FILE_ATTRIBUTE_TEMPORARY


combine them with the "or"

SetFileAttributes(PChar(FileName), FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_READONLY);

Author

Commented:
Thanks so much

Explore More ContentExplore courses, solutions, and other research materials related to this topic.