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?
 

Some videos you may like

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,047
Office Version
  1. 365
Platform
  1. Windows

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071
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:)
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,047
Office Version
  1. 365
Platform
  1. Windows

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071

ADVERTISEMENT

LOL well don't feel bad, i just used your advice on using AND to reveal the attributes.
 

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,028
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.
 

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071

ADVERTISEMENT

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.
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473
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?
 

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,028
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.
 

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071
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:
 

Watch MrExcel Video

Forum statistics

Threads
1,118,013
Messages
5,569,620
Members
412,283
Latest member
camgree
Top