GetAttr, SetAttr Functions

Syntax

% = GetAttr(pathname)

success% = SetAttr(pathname, attr) ( function)

SetAttr pathname, attr (command)

Included for compatibilty with GFA-BASIC 16:

% = FGATTR (pathname) (same as GetAttr)

success& = FSATTR (pathname, attr&) (same as SetAttr function)

Description

The function GetAttr returns the attributes of a file or a directory while SetAttr sets them. Following constants (values) are predefined:

FILE_ATTRIBUTE_NORMAL (0) - Normal file

FILE_ATTRIBUTE_READONLY(1) - Read-Only (write protected)

FILE_ATTRIBUTE_HIDDEN (2) - Hidden

FILE_ATTRIBUTE_SYSTEM (4) - System

FILE_ATTRIBUTE_DIRECTORY (16) - Directory

FILE_ATTRIBUTE_ARCHIVE (32) - Archive (reserved for Backups).

FILE_ATTRIBUTE_TEMPORARY (256) - Temporary file

FILE_ATTRIBUTE_OFFLINE (4096) - Offline

More values may be returned. These values can not be set using SetAttr, though.

64 encrypted file, set by EncryptFile
512 Joke file (file with holes)
1024 Reparse
2048 compressed
8192 Not contended index

If either of the functions fail, the return value is -1; the command version of SetAttr should be used within a Try-Catch block to catch any possible errors.

With GetAttr, to determine which attributes are set, use the And operator to perform a bitwise comparison of the value returned by the GetAttr function and the value of the individual file attribute you want. If the result is not zero, that attribute is set for the named file. For example, the return value of the following And expression is 16 if the directory exists:

If GetAttr("directory") And 16 Then // Directory exist!

GetAttr() returns the attributes of the last Dir[$].

Example

OpenW 1

// Read the contents of the current path

// and show: attribute,

// size of a file in KB, date, time, name

FullW 1

PrintScroll = True      ' activate scrolling

Local file$, a$, b$

Local Attr As Integer

file$ = Dir$("*", &H16)

While Len(file$) : a$ = ""

Attr = GetAttr(file$)

a$ = a$ + Iif(Attr And 32, "A", "-")

a$ = a$ + Iif(Attr And 16, "D", "-")

a$ = a$ + Iif(Attr And 4, "S", "-")

a$ = a$ + Iif(Attr And 2, "H", "-")

a$ = a$ + Iif(Attr And 1, "R", "-")

If Attr And 16 Then

a$ = a$ + " <Dir>"

Else

b$ = Str$(FileLen(file$))

b$ = Space$(8 - Len(b$)) + b$

a$ = a$ + Format(FileLen(file$), "* #######0")

a$ = a$ + b$

End If

a$ = a$ + " "

If file$ <> "." And file$ <> ".." Then

a$ = a$ + Format(FileDateTime(file$), "dd.mm.yyyy hh:nn:ss ")

End If

b$ = ShortFileName()

If b$ = "" : b$ = file$ : EndIf

a$ = a$ + Str$(b$, 14) + " "

Print a$

file$ = Dir

Wend

Do : Sleep : Until Me Is Nothing

Set the write protecting of the file "Test1.Dat"

Local a% = 25

Print App.Path & "\Test1.Dat"

BSave App.Path & "\Test1.Dat", V:a%, 4

SetAttr App.Path & "\Test1.Dat", GetAttr(App.Path & "\Test1.Dat") | 1

If GetAttr(App.Path & "\Test1.Dat") And 1

Print "write protected"

Else

Print "not write protected!"

EndIf

SetAttr App.Path & "\Test1.Dat", GetAttr(App.Path & "\Test1.Dat") Xor 1

If GetAttr(App.Path & "\Test1.Dat") And 1

Print "write protected"

Else

Print " not write protected!"

EndIf

Kill App.Path & "\Test1.Dat"  // Tidy up line

Remarks

To remove and set a write protection of a backup:

SetAttr "important.Bak", 0

CopyFile "important.Dat" Over To "important.Bak"

SetAttr "important.Bak", 1     ' activate write protection

The GetAttr function corresponds to the GetFileAttributes API function.

The SetAttr command corresponds to the SetFileAttributes API function

See Also

Dir, DirExist, FileAttr, SetFileDateTime, SetFileDateTimeAcces, SetFileDateTimeCreate, Touch.

{Created by Sjouke Hamstra; Last updated: 24/08/2021 by James Gaite}