Microsoft Docs

SetFileAttributes function

Namespace: Windows API

Header: Winbase.h (include Windows.h)

Library: Kernel32.lib

Syntax

BOOL SetFileAttributesW(
    LPCWSTR lpFileName,
    DWORD   dwFileAttributes
);

Parameters

NameDescription
lpFileName Pointer to a null-terminated string that specifies the name of the file or directory. Use the W version for Unicode strings.
dwFileAttributes File attribute flags. Combine multiple attributes using the bitwise OR operator. Common values:
  • FILE_ATTRIBUTE_READONLY
  • FILE_ATTRIBUTE_HIDDEN
  • FILE_ATTRIBUTE_SYSTEM
  • FILE_ATTRIBUTE_ARCHIVE
  • FILE_ATTRIBUTE_NORMAL
  • FILE_ATTRIBUTE_TEMPORARY
  • FILE_ATTRIBUTE_OFFLINE
  • FILE_ATTRIBUTE_NOT_CONTENT_INDEXED

Return value

If the function succeeds, the return value is nonzero. If it fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

Note: This function has both ANSI (SetFileAttributesA) and Unicode (SetFileAttributesW) variants. The ANSI version is less recommended for new development.

Example

#include <windows.h>
#include <stdio.h>

int main(void)
{
    LPCWSTR file = L"C:\\example\\test.txt";

    // Add the Hidden attribute
    if (SetFileAttributesW(file, FILE_ATTRIBUTE_HIDDEN))
        wprintf(L"Successfully set hidden attribute on %s\\n", file);
    else
        wprintf(L"Failed to set attribute. Error: %lu\\n", GetLastError());

    // Remove the Hidden attribute (set to normal)
    if (SetFileAttributesW(file, FILE_ATTRIBUTE_NORMAL))
        wprintf(L"Attributes cleared for %s\\n", file);
    else
        wprintf(L"Failed to clear attributes. Error: %lu\\n", GetLastError());

    return 0;
}

See also