File Attributes

Windows API Reference | Fundamentals | I/O

This topic describes file attributes, which are metadata associated with files and directories that define their characteristics and behavior.

On this page

Introduction

File attributes are crucial for managing file system objects on Windows. They provide information such as whether a file is read-only, hidden, a system file, or a directory. Understanding and manipulating these attributes is essential for various file operations, including security, organization, and application behavior.

The Windows operating system defines a set of standard attributes that can be applied to files and directories. These attributes are typically represented by flags that can be set or queried using system functions.

Common File Attributes

The following table lists some of the most common file attributes and their descriptions:

Attribute Description
FILE_ATTRIBUTE_READONLY The file or directory is read-only. Applications can read the file, but cannot write to it or delete it.
FILE_ATTRIBUTE_HIDDEN The file or directory is hidden. It is not included in an ordinary directory listing.
FILE_ATTRIBUTE_SYSTEM The file is a system file. This attribute is reserved for system use.
FILE_ATTRIBUTE_DIRECTORY This attribute indicates that the object is a directory.
FILE_ATTRIBUTE_ARCHIVE The file or directory is an archive file or directory. Applications use this to mark files for backup or removal.
FILE_ATTRIBUTE_NORMAL This attribute has no other meaning. It is used on Windows NT versions of 32-bit Windows and later.
FILE_ATTRIBUTE_TEMPORARY The file is a temporary file. File systems avoid writing data other than the logs to this file to improve performance.
FILE_ATTRIBUTE_COMPRESSED The file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the inherited attribute.
FILE_ATTRIBUTE_ENCRYPTED The file or directory is encrypted using the Encrypting File System (EFS). For a decrypted file, this indicates that all data blocks in the file are encrypted. For a directory, this means that encryption is the inherited attribute.

Accessing File Attributes

File attributes can be accessed and modified using various Windows API functions. The primary functions for this purpose are:

Here's a conceptual example of how you might retrieve file attributes:


#include <windows.h>
#include <iostream>

int main() {
    LPCWSTR fileName = L"MyDocument.txt";
    DWORD attributes = GetFileAttributes(fileName);

    if (attributes == INVALID_FILE_ATTRIBUTES) {
        std::cerr << "Error getting attributes for " << fileName << std::endl;
        return 1;
    }

    std::wcout << L"Attributes for " << fileName << L":" << std::endl;
    if (attributes & FILE_ATTRIBUTE_READONLY) {
        std::wcout << L"  - Read-Only" << std::endl;
    }
    if (attributes & FILE_ATTRIBUTE_HIDDEN) {
        std::wcout << L"  - Hidden" << std::endl;
    }
    if (attributes & FILE_ATTRIBUTE_DIRECTORY) {
        std::wcout << L"  - Directory" << std::endl;
    }
    // ... check for other attributes
    
    return 0;
}
            

Note: When using GetFileAttributes or SetFileAttributes, ensure you handle the INVALID_FILE_ATTRIBUTES return value to detect errors.

The SetFileAttributes function allows you to modify these attributes. For example, to make a file read-only:


#include <windows.h>
#include <iostream>

int main() {
    LPCWSTR fileName = L"MyImportantFile.dat";
    
    // Get current attributes to preserve others
    DWORD currentAttributes = GetFileAttributes(fileName);
    if (currentAttributes == INVALID_FILE_ATTRIBUTES) {
        std::cerr << "Error getting attributes for " << fileName << std::endl;
        return 1;
    }

    // Set the read-only attribute
    DWORD newAttributes = currentAttributes | FILE_ATTRIBUTE_READONLY;
    
    if (SetFileAttributes(fileName, newAttributes)) {
        std::wcout << L"Successfully set read-only attribute for " << fileName << std::endl;
    } else {
        std::cerr << L"Error setting read-only attribute for " << fileName << std::endl;
    }
    
    return 0;
}
            

Important: Always combine new attribute flags with existing attributes when using SetFileAttributes to avoid inadvertently clearing other important flags.