Writing File Attributes

The Code

Below is the code to call SetFileAttributes() to make C:\AREADING.TAB a read-only file with the archive bit set. The attributes are set, and then GetFileAttributes() is called to prove that the

declare function SetFileAttributes, GetFileAttributes

filename = 'C:\areading.tab'

EQU FILE_ATTRIBUTE_READONLY$ to 1

EQU FILE_ATTRIBUTE_HIDDEN$   to 2

EQU FILE_ATTRIBUTE_SYSTEM$   to 4

EQU FILE_ATTRIBUTE_DIRECTORY$ to 16

EQU FILE_ATTRIBUTE_ARCHIVE$   to 32

/* NORMAL is a file with neither the readonly, hidden, nor archive bits set */

EQU FILE_ATTRIBUTE_NORMAL$    to 128

EQU FILE_ATTRIBUTE_TEMPORARY$ to 256

rv = SetFileAttributes (filename, FILE_ATTRIBUTE_READONLY$ + FILE_ATTRIBUTE_ARCHIVE$)

attributes = GetFileAttributes (filename)

if bitand( attributes , FILE_ATTRIBUTE_READONLY$) then

  call msg( filename : ' is a readonly file.')

end

if bitand( attributes , FILE_ATTRIBUTE_HIDDEN$) then

  call msg( filename : ' is hidden file.')

end

if bitand( attributes , FILE_ATTRIBUTE_SYSTEM$) then

  call msg( filename : ' is system file.')

end

if bitand( attributes , FILE_ATTRIBUTE_DIRECTORY$) then

  call msg( filename : ' is directory.')

end

if bitand( attributes , FILE_ATTRIBUTE_ARCHIVE$) then

  call msg( filename : ' is an archive file.')

end

if bitand( attributes , FILE_ATTRIBUTE_NORMAL$) then

  call msg( filename : ' is a normal file.')

end

if bitand( attributes , FILE_ATTRIBUTE_TEMPORARY$) then

  call msg( filename : ' is a temporary file.')

end

call msg(@window , 'attributes = ': attributes)

Note how the values for the attribute bits are added. The attribute value returned from SetFileAttributes() is 33 (32 for the archive bit, plus 1 for the read-only bit).

The Windows API Declaration

The code above will not run until the declarations for GetFileAttributes() and SetFileAttributes() have been added. To add the declaration, do the following:

KERNEL32
LONG STDCALL GetFileAttributesA(LPCHAR) as GetFileAttributes
LONG STDCALL SetFileAttributesA(LPCHAR, ULONG) as SetFileAttributes
//....add any other declarations in KERNEL32 here.....

The function is aliased to its ANSI version GetFileAttributesA().

RUN DECLARE_FCNS 'DLL_APICALLS_KERNEL32'