How to check if file is directory based on it's attributes?

I know how to get file attributes, but I got problem.

DIRECTORY = 16 but ARCHIVE = 32, so if I check it like if attribute > 16 then directory, I would get false directories because 32 > 16

But directory can also have other attributes such as hidden/system/readonly/archive and so on, so check like if attribute == 16 would also fail 90% of times.

Is there way (that doesn't fail or show false directories) to check wether file is directory?

1
2
3
4
5
6
7
8
9
10
11
12
13
FILE_ATTRIBUTE_READONLY = 1
FILE_ATTRIBUTE_HIDDEN = 2
FILE_ATTRIBUTE_SYSTEM = 4
FILE_ATTRIBUTE_DIRECTORY = 16
FILE_ATTRIBUTE_ARCHIVE = 32
FILE_ATTRIBUTE_ENCRYPTED = 64
FILE_ATTRIBUTE_NORMAL = 128
FILE_ATTRIBUTE_TEMPORARY = 256
FILE_ATTRIBUTE_SPARSE_FILE = 512
FILE_ATTRIBUTE_REPARSE_POINT = 1024
FILE_ATTRIBUTE_COMPRESSED = 2048
FILE_ATTRIBUTE_OFFLINE = 4096
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192


Thanks in advane.
These values are chosen for a reason - they are powers of two
They are really made for bit testing

so

1
2
3
4
5
6
7
8
if (attribute_value_to_check & FILE_ATTRIBUTE_DIRECTORY)
{
    //is directory
}
else
{
    //not directory
}
Last edited on
Topic archived. No new replies allowed.