DIR statement : file attributes for Newbies

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

DIR statement : file attributes for Newbies

Postby Trinity » Oct 16, 2017 17:20

DIR statement : file attributes for Newbies

As a Newbie I have spent some time trying to find out about the "out_attrib" when using the DIR statement (ref. FB manual : https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDir)
I think that the manual is a bit thin on the information on the File Attributes return values of "out_attrib" when describing the use of DIR.
After some experimentation I have compiled a list of the return values for the six most basics attributes and their combinations , that is for :
"File" , "Directory" , "Archived" , "System" , "Hidden" and "Read-only" .
Here is the list :

out_attrib :

0 = &h00 = File
1 = &h01 = File + Read-only
2 = &h02 = File + Hidden
3 = &h03 = File + Read-only + Hidden
4 = &h04 = File + System
5 = &h05 = File + System + Read-only
6 = &h06 = File + System + Hidden
7 = &h07 = File + System + Hidden + Read-only

16 = &h10 = Directory
17 = &h11 = Directory + Read-only
18 = &h12 = Directory + Hidden
19 = &h13 = Directory + Read-only + Hidden
20 = &h14 = Directory + System
21 = &h15 = Directory + System + Read-only
22 = &h16 = Directory + System + Hidden
23 = &h17 = Directory + System + Hidden + Read-only

32 = &h20 = File + archived
33 = &h21 = File + archived + Read-only
34 = &h22 = File + archived + Hidden
35 = &h23 = File + archived + Read-only + Hidden
36 = &h24 = File + archived + System
37 = &h25 = File + archived + System + Read-only
38 = &h26 = File + archived + System + Hidden
39 = &h27 = File + archived + System + Hidden + Read-only

48 = &h30 = Directory + archived
49 = &h31 = Directory + archived + Read-only
50 = &h32 = Directory + archived + Hidden
51 = &h33 = Directory + archived + Read-only + Hidden
52 = &h34 = Directory + archived + System
53 = &h35 = Directory + archived + System + Read-only
54 = &h36 = Directory + archived + System + Hidden
55 = &h37 = Directory + archived + System + Hidden + Read-only

These six "out_attrib" values is with reference to the use of DIR as shown in example here that I made using manual example as basis for modification (Note : code will only work for files and directories having only the 6 attributes desribed) :

Code: Select all

'' Heavily modified manual example of using DIR function and retrieving attributes
'' Made by freebasic.net forum member "Trinity"
' -----------------------------------------------------
#include "fbgfx.bi"
Using FB ' namespace
dim as integer W,H
screeninfo W,H  'monitor dimensions
W=W-24 : H=H-24
screenres W,H,,, GFX_NO_FRAME ' temp
Width w\8, H\16 '' Use 8*16 font

' -----------------------------------------------------
Print  "   Dir = "; : Print CurDir : Print 
' -----------------------------------------------------

#include "dir.bi" '' provides constants to match the attributes against

'' set input attribute mask to allow files that are normal, hidden, system or directory
Const attrib_mask = fbNormal Or fbHidden Or fbSystem Or fbDirectory ' = &h37

Dim As UInteger out_attr,sortattrib,printpause '' unsigned integers , out_attr hold retrieved attributes
Dim As String fname, ISfbArchive, ISfileOrDir '' fname for file/directory name return
Dim As Integer filecount, dircount,t
Dim as string attributenames(1 to 7)

printpause=20 '  set number of records to show before print pause

Restore AttributeData : For t=1 to 7 : read attributenames(t) : next t  ' Reading attribute name data
AttributeData:
Data "fbReadOnly","fbHidden","fbReadOnly , fbHidden","fbSystem","fbSystem , fbReadOnly","fbSystem , fbHidden","fbSystem , fbReadOnly , fbHidden" ' Attribute name data

fname = Dir("*.*", attrib_mask, out_attr) '' Get first file name/attributes, according to supplied file spec and attribute mask

Print "File listing in " & CurDir & ":"

Do Until Len(fname) = 0 '' loop until Dir returns empty string
sortattrib=out_attr

    If (fname <> ".") And (fname <> "..") Then  '' ignore current and parent directory entries
        if sortattrib>=32 then ISfbArchive="fbArchive" : sortattrib = sortattrib-32  else ISfbArchive="" ' Checks if Archive or not
        if sortattrib>=16 then ISfileOrDir="Directory" : dircount+=1 : sortattrib = sortattrib-16  else ISfileOrDir="file" : filecount += 1 ' Checks if directory or file
        Print fname;  : locate ,40  : Print ;ISfileOrDir & right("      ",6*abs(ISfileOrDir="file")) ; ' Prints if Fname is of type file or directory
        locate ,50 : If sortattrib=0 then Print ;ISfbArchive else print ;ISfbArchive & right(" , ",3*abs(ISfbArchive<>""))& attributenames(sortattrib) ' prints attributes of Fname
        if (filecount+dircount) mod printpause=0 then SLEEP : While Inkey <> "": Wend ' Print pause if
    End If

    fname = Dir(out_attr) '' find next name/attributes

Loop

Print
Print "Found " & filecount & " files and " & dircount & " subdirs"

SLEEP
fxm
Posts: 9260
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DIR statement : file attributes for Newbies

Postby fxm » Oct 16, 2017 18:12

For me, the manual page is complete:
"Dir" page wrote:The following defined constants are used as bit-flags in attrib_mask and in out_attrib or *p_out_attrib. Their values can be combined to form a mask using Operator Or.
# define fbReadOnly &h01
# define fbHidden &h02
# define fbSystem &h04
# define fbDirectory &h10
# define fbArchive &h20
# define fbNormal (fbReadOnly or fbArchive)

For example:
Trinity wrote:35 = &h23 = File + archived + Read-only + Hidden
35 = &h23 = fbArchive Or fbHidden OR fbReadOnly
St_W
Posts: 1477
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: DIR statement : file attributes for Newbies

Postby St_W » Oct 16, 2017 18:19

The manual explicitly mentions that the out_attr value is a bit mask.

Your code is a lot more complicated than the example on the wiki.
I'd strongly recommend to extract attributes with binary logic as shown in the wiki. I'd even consider your implementation "bad style".
I'm wondering what additional information you're missing, as the wiki page even explains the basic usage of the binary operators to set, clear and test certain bits of the bit mask.
Further basic knowledge about bit masks is out of scope of the manual and e.g. can be found on Wikipedia: https://en.wikipedia.org/wiki/Mask_(computing)
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: DIR statement : file attributes for Newbies

Postby Trinity » Oct 16, 2017 18:24

fxm wrote:For me, the manual page is complete:
Good for you :-)

But as always , then what may seem obvious to the "trained eye" or the experienced programmer may not be equally obvious to the beginner / Newbie.
Personally I am always appreciative of the huge work that has already been done on documentation , thank you :-)
Anyway , should you ever want to include more information on return values for DIR use in the documentation then please feel free to copy my list and use it there as your own :-)
St_W
Posts: 1477
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: DIR statement : file attributes for Newbies

Postby St_W » Oct 16, 2017 18:38

Trinity wrote:Anyway , should you ever want to include more information on return values for DIR use in the documentation then please feel free to copy my list and use it there as your own
That list ist not needed and IMHO even misleading. The important thing is that you understand how bit masks work. The Wikipedia article I've linked above is a good start. Once you comprehend bit masks you won't need that list anymore.
Probably you should also read the articles about binary numbers and bitwise operations in general:
https://en.wikipedia.org/wiki/Binary_number
https://en.wikipedia.org/wiki/Bitwise_operation

Regarding newbies: Maybe we should add a link to that page in the wiki or an entry in the glossary pointing to the Wikipedia page? On the other hand it's really a basic concept that can be found in practically every programming language. IMHO users should be able to get information about such basic topics easily themselves (Google ...).
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: DIR statement : file attributes for Newbies

Postby Trinity » Oct 16, 2017 18:52

St_W wrote:The manual explicitly mentions that the out_attr value is a bit mask.
Sounds as critique to me , and the relevance to my list is what ?
St_W wrote:Your code is a lot more complicated than the example on the wiki.
And so ? , I haven't asked that the examples be swapped !
St_W wrote:I'd strongly recommend to extract attributes with binary logic as shown in the wiki. I'd even consider your implementation "bad style".
I do not yet master "binary logic as shown in the wiki" and have no intention having to spend time on learning about it now. (Have learned about bits but that's a long time ago) (I am also aware or the many advantages of mastering the binary forms better however that does not make me master it now).
Please feel free to frown upon and look down on programmers that do not master "binary logic as shown in the wiki"
St_W wrote:I'm wondering what additional information you're missing
The list as made by me for easy reference for beginners.
St_W wrote:Further basic knowledge about bit masks is out of scope of the manual and e.g. can be found on Wikipedia: https://en.wikipedia.org/wiki/Mask_(computing)
That's great , anyone interested in learning about that now ought to go have a look
St_W wrote:The manual explicitly mentions that the out_attr value is a bit mask.
<Snip>
I'm wondering what additional information you're missing, as the wiki page even explains the basic usage of the binary operators to set, clear and test certain bits of the bit mask.
Yes , I understood that rather soon reading the manual and looking at code example in the manual, but unless people have some fascistoid hate against ordinary numbers then I do not see any problem or harm in presenting the values in ordinary numbers also , I even recognized the binary connection by give the Hex reference also in the list - as in the manual
St_W wrote:Your code is a lot more complicated than the example on the wiki.
I will take that as a compliment ;-) :-D
St_W wrote:I'd strongly recommend to extract attributes with binary logic as shown in the wiki. I'd even consider your implementation "bad style"
And you think that "binary logic as shown in the wiki" is less complicated to the Newbie than ordinary numbers ?? ;-) :-D
St_W
Posts: 1477
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: DIR statement : file attributes for Newbies

Postby St_W » Oct 16, 2017 19:02

Trinity wrote:I do not yet master "binary logic as shown in the wiki" and have no intention having to spend time on learning about it now.

You definitely should. Binary operations is what a computer is built upon and how it works. Learning programming a computer without knowing the basics of how a computer works is no good idea IMHO.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: DIR statement : file attributes for Newbies

Postby Trinity » Oct 16, 2017 19:06

St_W wrote:That list ist not needed and IMHO even misleading.
Well , then FXM will most likely not put it in the Manual then. By the way , I do not know if you have noticed this , but this thread is in the Tips and tricks and subject is marked for Newbies. So 1) I have *not* posted this as a manual suggestion over in the documentation thread !!!! and subject *IS* marked newbies !!! and , 2) I hardly see how your comments should have any greater bearings on my opinion since you are not a newbie and this thread is directed at newbies.
St_W wrote: The important thing is that you understand how bit masks work.
Yes , hurray for people who understands that ;-)
St_W wrote: The Wikipedia article I've linked above is a good start. Once you comprehend bit masks you won't need that list anymore.
Probably you should also read the articles about binary numbers and bitwise operations in general:
https://en.wikipedia.org/wiki/Binary_number
https://en.wikipedia.org/wiki/Bitwise_operation
Thank you I will try to remember to use Wikipedia (as I so often do) if I ever want to look up "Binary number" and "Bitwise operation"
St_W wrote:Regarding newbies: Maybe we should add a link to that page in the wiki or an entry in the glossary pointing to the Wikipedia page? On the other hand it's really a basic concept that can be found in practically every programming language. IMHO users should be able to get information about such basic topics easily themselves (Google ...).
I have not asked any of that. It's your angle on all this that keeps you going on about a subject that I am not interested in in the moment. I am a beginner in FreeBASIC !!! , you know ! , beginner !!!
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: DIR statement : file attributes for Newbies

Postby Trinity » Oct 16, 2017 19:11

St_W wrote:You definitely should. Binary operations is what a computer is built upon and how it works. Learning programming a computer without knowing the basics of how a computer works is no good idea IMHO.
I know , and I do understand, and having programmed at bit in assembler years back I may still understand more than you think , but there is a very long way from knowing that and understanding that to be able to use all the binary tools that there are in FB ! I am still a beginner at FreeBASIC and I have so much more to do other than learning what for me is the more advanced subjects of FB.
fxm
Posts: 9260
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DIR statement : file attributes for Newbies

Postby fxm » Oct 16, 2017 19:15

Code: Select all

attrib_mask = 35
is bad coding.

Code: Select all

# include "dir.bi"

attrib_mask = fbArchive Or fbHidden OR fbReadOnly
is right coding.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: DIR statement : file attributes for Newbies

Postby Trinity » Oct 16, 2017 19:29

fxm wrote:

Code: Select all

attrib_mask = 35
is bad coding.

Code: Select all

# include "dir.bi"
attrib_mask = fbArchive Or fbHidden OR fbReadOnly
is right coding.
Thank you , but where did I use "attrib_mask = 35" ?` ? , I am not saying that I wouldn't write code as bad as that , I am just asking where I did ?
I did the opposite I think , I extracted all the out_attrib values and listed then as both ordinary numbers and as Hex values , to make it much more easy for people (newbies) to understand the relationship between out_attrib and the actual attributes.
You (and St_W) criticizing me for all this is like criticizing a person for explaining in ordinary English what goes on in a computer program.
But anyway , thank you for the inspiration , I might add your suggestion (what may have crossed my mind but what I have not already done as far as I know) to the list over bad habits to pick up ;-) :-D
Josep Roca
Posts: 449
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: DIR statement : file attributes for Newbies

Postby Josep Roca » Oct 16, 2017 19:32

> Yes , hurray for people who understands that ;-)

It is not that hard.

IF (attrib_mask AND fbDirectory) = fbDirectory THEN ... it is a directory.

No need to having to use a long list of attribute values.
St_W
Posts: 1477
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: DIR statement : file attributes for Newbies

Postby St_W » Oct 16, 2017 19:40

Trinity wrote:By the way , I do not know if you have noticed this , but this thread is in the Tips and tricks and subject is marked for Newbies. So 1) I have *not* posted this as a manual suggestion over in the documentation thread !!!! and subject *IS* marked newbies !!! and , 2) I hardly see how your comments should have any greater bearings on my opinion since you are not a newbie and this thread is directed at newbies.

Indeed, I haven't noticed that this thread is in the "Tips and tricks" section. But that does not make it better; I'd even say that's even worse. It is very important that newbies learn to do things the correct way from the beginning on. Relearning things should be avoided as it causes way more work than learning it correctly in the first place. That is why I strongly recommended (or even insisted on) learning how to use bit masks first. You don't even have to know a lot about bit masks if you just use the code fragments from the manual.

The correct way to extract the properties could be implemented like this:

Code: Select all

#include "dir.bi" ' provides constants to match the attributes against

Dim As UInteger out_attr ' unsigned integer to hold retrieved attributes

Dim as Boolean IsReadOnly, IsHidden, IsSystem, IsArchive, IsDirectory

IsReadOnly = (out_attr And fbReadOnly) <> 0
IsHidden = (out_attr And fbHidden  ) <> 0
IsSystem = (out_attr And fbSystem  ) <> 0
IsArchive = (out_attr And fbArchive ) <> 0
IsDirectory = (out_attr And fbDirectory) <> 0
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: DIR statement : file attributes for Newbies

Postby Trinity » Oct 16, 2017 19:55

St_W wrote:That is why I strongly recommended (or even insisted on) learning how to use bit masks first. You don't even have to know a lot about bit masks if you just use the code fragments from the manual.
In my opinion you are being very unreasonable and close to showing zero understanding for how it is to have to learn FB coming with my background.
St_W wrote:The correct way to extract the properties could be implemented like this:
Did I ever mention(yes I did in another thread) that I am not good at Boolean either why I am trying to adopt the use of AndAlso and OrElse rather than relying on the Boolean logic operators which I do not master yet.
(Please spare me the long hailing speech about how knowing Boolean logic is indispensable for wring good computer programs. But the great thing for me is that it's me sitting in-front of my screen so I can learn the stuff when and how I choose. And I don't have to care if you think that what I write of computer programs you would think that you could do so much better than I.)
(If your intention have been to help in this thread then you have failed miserably because you have come across as criticizing only from the start to end)(If not criticizing me and what I have done then criticizing my abilities and vice versa)
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: DIR statement : file attributes for Newbies

Postby Trinity » Oct 16, 2017 20:15

By the way , this is a link to only one of *many* pages I visited this afternoon (I Googled the subject intensely to study it) when studying the file attribute thing : http://www.pcguide.com/ref/hdd/file/fat ... tes-c.html (So I have been aware of the bit notation thing during the whole of this discussion)
And not that I generally consider it any business of the participants here but then I took the liberty of saving that page to look at later , I did that early afternoon. My point is that people that are criticizing me here , besides having zero understanding for how much slack I have to try to pick up (and how bad my situation is) , also have zero understanding of how much I wish to learn more and how much I try to prepare to do so.
But in the end of the day then I get very little done and I have to do what needs to be done and what I want to do the most rather than satisfying other people...

And... P.S. :
For me it were actually a nice progress to get so much more further as to get that much better grasp of the DIR and out_attr that I have done as reflected in my initial post in this thread :-D

Return to “Tips and Tricks”

Who is online

Users browsing this forum: MSN [Bot] and 2 guests