Skip to content

Win32 Get Attributes

A C program that prints every set file attribute for a given file using GetFileAttributesEx.

Published Feb 8, 1996

The quick way to see what attributes a file carries. GetFileAttributesEx (with GetFileExInfoStandard) returns the attribute bitmask, and each flag is tested individually so you get a readable list - Archive, Compressed, Directory, Hidden, Normal, Offline, Readonly, System, Temporary - rather than an opaque number. Useful in file managers, backup tools deciding what to skip, or diagnosing why an operation failed on a read-only or system file.

#include <Windows.h>
#include <stdio.h>

int main(void)
{
    char FileName[128];
    unsigned Attrib;
    sprintf(FileName,"TestFile.Com");

    if(GetFileAttributesEx(FileName,GetFileExInfoStandard,&Attrib))
    {
        if(Attrib & FILE_ATTRIBUTE_ARCHIVE)
            printf("Archive.\n");
        if(Attrib & FILE_ATTRIBUTE_COMPRESSED)
            printf("Compressed.\n");
        if(Attrib & FILE_ATTRIBUTE_DIRECTORY)
            printf("Directory.\n");
        if(Attrib & FILE_ATTRIBUTE_HIDDEN)
            printf("Hidden.\n");
        if(Attrib & FILE_ATTRIBUTE_NORMAL)
            printf("Normal.\n");
        if(Attrib & FILE_ATTRIBUTE_OFFLINE)
            printf("OffLine.\n");
        if(Attrib & FILE_ATTRIBUTE_READONLY)
            printf("Readonly.\n");
        if(Attrib & FILE_ATTRIBUTE_SYSTEM)
            printf("System.\n");
        if(Attrib & FILE_ATTRIBUTE_TEMPORARY)
            printf("Temporary.\n");
    }

    return 0;
}

Two notes: the function as written declares Attrib but passes its address where BY_HANDLE_FILE_INFORMATION* belongs - the plain GetFileAttributes(FileName) call returns the same DWORD directly and is the simpler choice; and this pairs with the Win32 Set Attributes article, which shows how to change the bits you just read.

An unhandled error has occurred. Reload 🗙