GetAttr Returns 22 and 17

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071
I was trying to make use of GetAttr and the help file states GetAttr will return the following:

vbNormal 0 Normal.
vbReadOnly 1 Read-only.
vbHidden 2 Hidden.
vbSystem 4 System file. Not available on the Macintosh.
vbDirectory 16 Directory or folder.
vbArchive 32 File has changed since last backup. Not available on the Macintosh.
vbAlias 64 Specified file name is an alias. Available only on the Macintosh.

But I have also seen it return 17 & 22. Does anyone know what the vbfriendly names are or these or anything about them?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Ahhh. now I get it. 22 is a combination of Directory (16) Hidden(2) and System(4). I didn't realize it added them all up. Thanks:)
 
Upvote 0
LOL well don't feel bad, i just used your advice on using AND to reveal the attributes.
 
Upvote 0
Yes! In fact the correct (and safe) way to use information returned in individual bits is to AND the returned value with the constant. An example would be
Code:
if (rslt and vbNormal) =vbnormal then...
if (rslt and vbReadOnly) =vbreadonly then...
if (rslt and vbHidden) =vbHidden then...
...
If you use the above method, you don't need to know what bit each attribute corresponds to nor do you have to worry about MS adding additional bits of information to the returned result. Your code won't know about them but neither will it fault. It will just gracefully ignore the additional information.

Edit: Added parenthesis to get the correct operator precedence.
Oorang said:
LOL well don't feel bad, i just used your advice on using AND to reveal the attributes.
 
Upvote 0
Although I did notice something odd, if I am using AND in conjunction with <>:
Code:
If GetAttr(X) AND vbDirectory <> 0 then
I have to enclose it in parentheses or it won't work:
Code:
If (GetAttr(X) AND vbDirectory) <> 0 then
Not sure what's up with that.
 
Upvote 0
Oorang said:
Although I did notice something odd, if I am using AND in conjunction with <>:
Code:
If GetAttr(X) AND vbDirectory <> 0 then
I have to enclose it in parentheses or it won't work:
Code:
If (GetAttr(X) AND vbDirectory) <> 0 then
Not sure what's up with that.

Shouldn't it be

If GetAttr(X) <>0 AND vbDirectory <> 0 then

unless GetAttr() is a Boolean array?
 
Upvote 0
Nothing odd about it. It's normal operator precedence. In fact, while typing my post something was bugging me but I didn't follow through on it.

Oorang said:
Although I did notice something odd, if I am using AND in conjunction with <>:
Code:
If GetAttr(X) AND vbDirectory <> 0 then
I have to enclose it in parentheses or it won't work:
Code:
If (GetAttr(X) AND vbDirectory) <> 0 then
Not sure what's up with that.
 
Upvote 0
Shouldn't it be

If GetAttr(X) <>0 AND vbDirectory <> 0 then

unless GetAttr() is a Boolean array?
The use of and in this case is doing a bitwise comparison. vbDirectory is a constant of 16 and GetAttr(x) will return a number that is the sum of all the file attribute constants. So to see if vbDirectory is set and you do GetAttr(X) which returns 22, to see if vbDirectory is part of that 22 you do
GetAttr(X) AND vbDirectory which returns 16 if it is and 0 if it isn't. However for some reason you can do If (GetAttr(X) AND vbDirectory) = 16 with no problems but to make If GetAttr(X) AND vbDirectory <> 0 work you need the parentheses.
Why?
No Clue.
But vbDirectory will always = 16 so you can't do If GetAttr(X) <>0 AND vbDirectory <> 0 because then you are looking at each one individually instead of compared. (I think) :wink:
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,509
Members
448,967
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top