It isn't necessary to understand binary numbers and bitmasks to use either file attributes or bitmasks in OOo Basic, so don't panic; simply skip the parts that make your head spin. Understanding this material, however, makes it simpler to understand what is happening with file attributes and how to use them.
The file and directory attributes in Table 3 were strategically chosen to have a nice property when written in base 2 (see Table 5 ) -each attribute has only one bit set. Zero is a special case-it has no bits set.
Decimal Attribute | Binary Attribute | Description | Comment |
---|---|---|---|
00 | 0000 0000 | Normal | No bits set |
01 | 0000 0001 | Read-Only | Bit 1 set |
02 | 0000 0010 | Hidden | Bit 2 set |
04 | 0000 0100 | System | Bit 3 set |
08 | 0000 1000 | Volume | Bit 4 set |
16 | 0001 0000 | Directory | Bit 5 set |
32 | 0010 0000 | Archive | Bit 6 set |
Use GetAttr to obtain the attributes of a file or path. If the file or path is a directory, then bit 5 is set. If the file or path is read-only, then bit 1 is set. A returned attribute of 0 means that no bits are set and that this is a normal file. Consider an attribute value of 33, which in binary is 0010 0001. Bit 1 is set, so this is read-only. Bit 6 is set, so this file has changed since it was last archived. You can see that you don't need to know how to convert a decimal number into a binary number. However, you do need to know how to write a macro to determine which bits are set and which bits are not set. Use the AND operator to determine which bits are set. With AND, two things must both be true for the answer to be true. For example, my flashlight works if it has a light bulb AND it has batteries.
The AND operator works with numbers by performing this logical operation on each of the bits. For example, "3 AND 5" represented as base 2 is "0011 AND 0101 = 0001". Bit 1-the bit in the rightmost position-in each number is equal to 1, so bit 1 in the result is also 1. All of the other bits do not have corresponding 1s in the same position, so all of the other bits in the result equal zero.
Now I'll apply this idea to the problem at hand. If the numeric value of an attribute is not zero, then at least one property is set. Given this, you can then check each attribute as illustrated by the example in Table 6 .
Read-Only | Hidden | System | Volume | Directory | Archive |
---|---|---|---|---|---|
10 0001 | 10 0001 | 10 0001 | 10 0001 | 10 0001 | 10 0001 |
AND 00 0001 | AND 00 0010 | AND 00 0100 | AND 00 1000 | AND 01 0000 | AND 10 0000 |
(1) 00 0001 | (0) 00 0000 | (0) 00 0000 | (0) 00 0000 | (0) 00 0000 | (32)10 0000 |
To do this in OOo Basic, use code similar to the following:
If TheAttribute = 0 Then REM No attributes set Else If (TheAttribute AND 1) = 1 Then ... 'Read-Only file: bit 1 is set If (TheAttribute AND 16) = 16 Then ... 'Directory: bit 5 is set. If (TheAttribute AND 4) <> 0 Then ... 'Another way to code the same logic. End If
Each file and directory has an attribute defined as these bit patterns. If a bit in the attribute that corresponds to a particular property is set, then the file has that property. Performing the AND function with the individual bit positions determines if the file has that property. The function FileAttributeString in Listing 4 uses this method.
To set the archive bit and read-only bit on a file, either call the SetAttr function once for each bit or combine the bits and call the function once. Use the OR operator to combine bit patterns. With the OR operator, if either bit is set, the resulting bit is a 1. To set the read-only and the archive bits, use "1 OR 32".