GetFileAttributesW function

DWORD GetFileAttributesW(
    _In_ LPCWSTR lpFileName
);

File Type

Platform: Windows

Header:

Fileapi.h

Library:

Kernel32.lib

DLL:

Kernel32.dll

Retrieves the attributes of a specified file or directory.

Syntax


DWORD GetFileAttributesW(
  [in] LPCWSTR lpFileName
);
        

Parameters

Parameter Type Description
lpFileName [in] LPCWSTR The name of the file or directory. A trailing backslash that is appended to the path of a directory does not change the returned behavior.

Return value

If the function succeeds, the return value is the attribute value for the specified file or directory. If the function fails for any other reason, the return value is INVALID_FILE_ATTRIBUTES. To get extended error information, call GetLastError.

The attribute value is a bitmask of one or more of the file attribute flags.

Remarks

To get the attributes of a file or directory, call the GetFileAttributes function. This function returns an attribute mask that specifies the file's attributes. You can then use this mask to determine the file's attributes.

To get extended error information, call GetLastError.

The lpFileName parameter can specify a relative path or a fully qualified path. The maximum string size for a path is MAX_PATH (32,767 characters). However, paths that are shorter than 32,767 characters are recommended. Use `\\?\` global root prefix for paths longer than `MAX_PATH`.

Examples

For a code example of using this function, see Getting File Attributes.


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

int main() {
    LPCWSTR fileName = L"example.txt"; // Replace with your file name
    DWORD attributes = GetFileAttributesW(fileName);

    if (attributes == INVALID_FILE_ATTRIBUTES) {
        std::wcerr << L"Could not get attributes for " << fileName << std::endl;
        std::wcerr << L"Error code: " << GetLastError() << std::endl;
        return 1;
    }

    std::wcout << L"Attributes for " << fileName << L":" << std::endl;

    if (attributes & FILE_ATTRIBUTE_ARCHIVE) std::wcout << L"- Archive" << std::endl;
    if (attributes & FILE_ATTRIBUTE_COMPRESSED) std::wcout << L"- Compressed" << std::endl;
    if (attributes & FILE_ATTRIBUTE_DIRECTORY) std::wcout << L"- Directory" << std::endl;
    if (attributes & FILE_ATTRIBUTE_ENCRYPTED) std::wcout << L"- Encrypted" << std::endl;
    if (attributes & FILE_ATTRIBUTE_HIDDEN) std::wcout << L"- Hidden" << std::endl;
    if (attributes & FILE_ATTRIBUTE_NORMAL) std::wcout << L"- Normal" << std::endl;
    if (attributes & FILE_ATTRIBUTE_OFFLINE) std::wcout << L"- Offline" << std::endl;
    if (attributes & FILE_ATTRIBUTE_READONLY) std::wcout << L"- Read-only" << std::endl;
    if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) std::wcout << L"- Reparse Point" << std::endl;
    if (attributes & FILE_ATTRIBUTE_SPARSE_FILE) std::wcout << L"- Sparse File" << std::endl;
    if (attributes & FILE_ATTRIBUTE_SYSTEM) std::wcout << L"- System" << std::endl;
    if (attributes & FILE_ATTRIBUTE_TEMPORARY) std::wcout << L"- Temporary" << std::endl;
    if (attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) std::wcout << L"- Not Content Indexed" << std::endl;
    if (attributes & FILE_ATTRIBUTE_ENCRYPTED) std::wcout << L"- Encrypted" << std::endl;

    return 0;
}
        

Requirements

Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header Fileapi.h (include Fileapi.h)
Library Kernel32.lib
DLL Kernel32.dll
Unicode and ANSI names Windows.h on Windows 2000 and later.

See also