Getting file atributes (FilesEx and Bitwise)

Mr_Gee

Active Member
Licensed User
Hi All,

I've been wrestling with this for a couple of day now, and it's really annoying me.

I'm trying to get and set the file properties, but i'm getting very inconsistent results..

I want to make sure the file is not readonly, hidden, system file etc.

According to Agraham the FilesEx needs the Bitwise.dll to be able to view the properties

I can understand that a file has a couple of bytes in which the properties are set, but i cant seem to get this info :-(

Can anyone point me in the right direction?
Below is a part of the code I used to test

B4X:
Sub read
   bit.New1
   Fex.New1
   item1 = Fex.FileGetAttributes(FileLoc& "\test.txt")
   listbox1.Clear
   listbox1.Add("readonly - "&bit.GetBit(item1,1))
   listbox1.Add("hidden - "&bit.GetBit(item1,2))
   listbox1.Add("system - "&bit.GetBit(item1,4))
   listbox1.Add("normal - "&bit.GetBit(item1,128))
End Sub    
Sub write   
   bit.New1
   Fex.New1
Fex.FileSetAttributes(FileLoc& "\test.txt",bit.SetBit(1,0))   
Fex.FileSetAttributes(FileLoc& "\test.txt",bit.SetBit(2,0))   
Fex.FileSetAttributes(FileLoc& "\test.txt",bit.SetBit(4,0))
Fex.FileSetAttributes(FileLoc& "\test.txt",bit.SetBit(128,0))
End Sub
I've also tried bit.GetBit(X,Y) and bit.GetBit(Y,X) (reverse the two)
also didn't do much ..

:sign0085:

Thanks!
 

agraham

Expert
Licensed User
Firstly your bit numbering is wrong, you are using the numeric value of a bit and not the bit position. It should be -
B4X:
   item1 = FilesEx1.FileGetAttributes(AppPath & "\test1.txt")
   msg = item1 & CRLF
   msg = msg & "readonly - "& bit.GetBit(item1,0) & CRLF
   msg = msg & "hidden - "  & bit.GetBit(item1, 1) & CRLF
   msg = msg & "system - " & bit.GetBit(item1, 2) & CRLF
   msg = msg & "normal - " & bit.GetBit(item1, 7)   & CRLF
Bit No, Value 0,1 : 1,2 : 2,4 : 3,8 : 4,16 : 5, 32 : 6,64 : 7,128 ...

Secondly you can't set attributes individually, they are all set at at once. Set the bits that you want and do a single Set.

B4X:
   item1 = FilesEx1.FileGetAttributes(AppPath & "\test1.txt")
   item1 = Bit.SetBit(item1, 0) ' original attributes plus the readonly bit
   FilesEx1.FileSetAttributes(AppPath & "\test1.txt",item1) ' make it readonly
 

Mr_Gee

Active Member
Licensed User
Thanks Agraham,

Just so I understand correctly in your example

item1 holds the bits
bit.GetBit(item1,0)
gets the value of bit 0 in item1
.....

bit.GetBit(item1, 7)
gets the value of bit 7 in item1

etc.

so basically it it states bit.GetBit(input_stream , bit_index )

Do the bits look look this : item1 = "11111111" or like
item1 = "1,2,4,16,32,64,128"

If its the first so this makes sense, then they look a bit like a dipswitch ...
If it is the second, I'm a bit unsure...

what doesn't make sense is the setting of the attributes..
why is it "Bit.SetBit(item1, 0)" ?

When doing a MsgBox(item1) I also saw some different numbers
32 & 33 etc..
Is this the sum of the set bits in item1? (just like a binary clock,adding them together?)

Bit.SetBit(item1, 7)
would be readonly, hidden & system set to true


Thanks for your help Agraham
 

agraham

Expert
Licensed User
Do the bits look look this : item1 = "11111111" or like
item1 = "1,2,4,16,32,64,128"
If its the first so this makes sense, then they look a bit like a dipswitch
Yes, that is how numbers are held in a computer. Binary numeral system - Wikipedia, the free encyclopedia
what doesn't make sense is the setting of the attributes..

why is it "Bit.SetBit(item1, 0)" ?
That is saying "set the bit position 0 of the number in item1". You would use ClrearBit to set it to 0.


When doing a MsgBox(item1) I also saw some different numbers
32 & 33 etc..
Is this the sum of the set bits in item1? (just like a binary clock,adding them together?)
Yes, it would be the sum of the values of each bit position.

Bit.SetBit(item1, 7)
would be readonly, hidden & system set to true
No. That just sets bit position 7 (128) to 1 but "FilesEx1.FileSetAttributes(AppPath & "\test1.txt",7) " would do that. You must distinguish between the individual bits that GetBit and ClearBit deal with and the number that a collection of bits represents.
 

Mr_Gee

Active Member
Licensed User
Originally Posted by Mr_Gee View Post
Do the bits look look this : item1 = "11111111"
Yes, that is how numbers are held in a computer
So a readonly file would be 10000000 ( position 1 set to true)
and a normal file would be 00000001 ( position 7 set to true)
hidden & readonly would be 11000000 ( position 1 & 2 set to true)

No. That just sets bit position 7 (128) to 1 but "FilesEx1.FileSetAttributes(AppPath & "\test1.txt",7) " would do that.
So SetBit only allows you to set a certain bit (attribute) to true,
to set it to false I would need to use bit.ClearBit(item1,XXX)
where XXX is the position that will be set...

You must distinguish between the individual bits that GetBit and ClearBit deal with and the number that a collection of bits represents.
Is it correct to assume that the two are the same (just more specific)?
e.g.
a readonly file saying ClearBit(item1,1) or
"FilesEx1.FileSetAttributes(AppPath & "\test1.txt",7)"

for a readonly & hidden file using the ClearBit(item1,1) would make it writable not visible while "FilesEx1.FileSetAttributes(AppPath & "\test1.txt",7)" would do both?

I'm a huge :sign0104: in this area...sorry
 

agraham

Expert
Licensed User
So a readonly file would be 10000000 ( position 1 set to true)
and a normal file would be 00000001 ( position 7 set to true)
hidden & readonly would be 11000000 ( position 1 & 2 set to true)
Conventionally binary numbers are writen with the same significance order as decimals and bit positions count from 0 so
...00000001 = readonly - position 0 not 1 set true
...00000011 = hidden and readonly - positions 0 and 1 set true
...10000000 = normal - position 7 set true - I have not seen this set
So SetBit only allows you to set a certain bit (attribute) to true,
to set it to false I would need to use bit.ClearBit(item1,XXX)
where XXX is the position that will be set...
Yes
Is it correct to assume that the two are the same (just more specific)?
e.g.
a readonly file saying ClearBit(item1,1) or
"FilesEx1.FileSetAttributes(AppPath & "\test1.txt",7)"
No. One is clearing a bit in a variable, the other is assigning a set of attributes to a file.

for a readonly & hidden file using the ClearBit(item1,1) would make it writable not visible while "FilesEx1.FileSetAttributes(AppPath & "\test1.txt",7)" would do both?
It's bit 0 not bit 1 that is the readonly attribute bit. Assuming item1 contained the original attribute set then ClearBit(item1,0) would make the file writeable but not visible once reassigned to the file. Assigning 7 to the file will make it readonly, hidden and system.
 

Mr_Gee

Active Member
Licensed User
...00000001 = readonly - position 0 not 1 set true
...00000011 = hidden and readonly - positions 0 and 1 set true
...10000000 = normal - position 7 set true - I have not seen this set
so the combined values for the above would be
1
3
128

Assigning 7 to the file will make it readonly, hidden and system.
because that is the sum of 1, 2 and 4 :sign0060:

so "FilesEx1.FileSetAttributes(AppPath & "\test1.txt",128) " Clears all attributes...

I'm getting there :)

I've attached my test file, maybe someone else can benefit from it
I entered 0 & 7 , if I hit "set" then "read" it gives me a value of
128 -> normal = true


Thanks for your great explanation :sign0188:
Let me get you a beer
 

Attachments

agraham

Expert
Licensed User
so "FilesEx1.FileSetAttributes(AppPath & "\test1.txt",128) " Clears all attributes...
In a real app it is better to always read the attributes and just modify the ones you want to change. There are other attributes, the ones in bit positions greater than 3, that you should probably not meddle with unless you are going to delete the file immediately.
 

Mr_Gee

Active Member
Licensed User
In a real app it is better to always read the attributes and just modify the ones you want to change. There are other attributes, the ones in bit positions greater than 3, that you should probably not meddle with unless you are going to delete the file immediately.
That's good advice

something like
B4X:
item1 = FilesEx1.FileGetAttributes(AppPath & "\test1.txt")
item1 = Bit.ClearBit(item1, 0) ' removes readonly attribute
item1 = Bit.ClearBit(item1, 1) '  ' removes readonly attribute
item1 = Bit.ClearBit(item1, 4) '  ' removes System attribute
FilesEx1.FileSetAttributes(AppPath & "\test1.txt",item1)
:)
 

agraham

Expert
Licensed User
B4X:
item1 = FilesEx1.FileGetAttributes(AppPath & "\test1.txt")
item1 = Bit.ClearBit(item1, 0) ' removes readonly attribute
item1 = Bit.ClearBit(item1, 1) '  ' removes readonly attribute
item1 = Bit.ClearBit(item1, 4) '  ' removes System attribute
FilesEx1.FileSetAttributes(AppPath & "\test1.txt",item1)
Nearly, but bit 2 is the system bit, you have its value there!
 

Mr_Gee

Active Member
Licensed User
cr*p.. so close

I was looking at the FilesEx helpfile, indeed 4 is Directory

I think I get it now, thanks again for your help and time.

I'm going to play around a bit more
 
Top