Public Group

# WIN32_FIND_DATA Question

This topic is 4882 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

One of the members of the struct WIN32_FIND_DATA is a DWORD dwFileAttributes, which can be one or more values. How can I test if one of the values is true? For example, how would I test if a file is a directory? You can't use == since it can be more than one value. [Edited by - v0dKA on January 8, 2005 4:29:56 PM]

##### Share on other sites
You want to use the bitwise AND operator (&) like so:

WIN32_FIND_DATA fd;if ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {  // Is a directory}else {  // Is not}

 You might want to check out bitstrings and bitmasks.
The reason this works is that ANDing the two together produces a non-zero value if the one of the attributes is a directory.
Say you have the following bitstring:
11010110
and you wanted to check that the second bit was true (1), you would AND it with
00000010
which would result precisely with 00000010, which is non-zero (TRUE). (Remeber that only TRUE AND TRUE results in TRUE)

If the original bitstring had been 11010100, then ANDing it with 00000010 would have resulted in 00000000 (FALSE)

Hope that helps.

##### Share on other sites
Yes, it does. And just to make sure, if, let's say, the second bit indeed is the directory bit, then FILE_ATTRIBUTE_DIRECTORY would be #defined 00000010?

Also, what do the files "." and ".." represent? I know one of them is the parent directory, but which one, and what's the other one?

##### Share on other sites
Well, actually it is defined as 0x00000002, because it's in hex (I don't know that there are "binary literals" in C++, someone enlighten me if there is), but that is the same as 000...000010, yes.

"." means current directory
".." means parent directory (directory above)

• 23
• 10
• 19
• 15
• 14