SetFileAttributes

The SetFileAttributes function sets the attributes of a file or directory.

Syntax

BOOL SetFileAttributes(
  LPCTSTR lpFileName,
  DWORD   dwFileAttributes
);

Parameters

Parameter Description
lpFileName A pointer to a null-terminated string that specifies the name of the file or directory whose attributes are to be set.
dwFileAttributes The file attributes to set for the file. This parameter can be a combination of the following values:
  • FILE_ATTRIBUTE_ARCHIVE
  • FILE_ATTRIBUTE_COMPRESSED
  • FILE_ATTRIBUTE_DIRECTORY
  • FILE_ATTRIBUTE_ENCRYPTED
  • FILE_ATTRIBUTE_HIDDEN
  • FILE_ATTRIBUTE_NORMAL
  • FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
  • FILE_ATTRIBUTE_OFFLINE
  • FILE_ATTRIBUTE_READONLY
  • FILE_ATTRIBUTE_REPARSE_POINT
  • FILE_ATTRIBUTE_SPARSE_FILE
  • FILE_ATTRIBUTE_SYSTEM
  • FILE_ATTRIBUTE_TEMPORARY
  • FILE_ATTRIBUTE_ENCRYPTED

Return Value

If the function succeeds, the return value is nonzero.

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

Remarks

When setting file attributes, you can combine multiple attribute flags using the bitwise OR operator (|).

You cannot set the FILE_ATTRIBUTE_DIRECTORY attribute using this function. To set the attributes of a directory, you must use the SetDirectoryAttributes function (which is not part of the core Win32 API and might refer to alternative methods).

If you attempt to set an attribute that is not allowed (e.g., setting FILE_ATTRIBUTE_READONLY on a directory), the function will fail and GetLastError will return an appropriate error code.

Note: For compatibility with previous versions of Windows, the function may accept the FILE_ATTRIBUTE_NORMAL flag. However, this flag is not a valid attribute to set, and it is recommended to use other flags instead.
Tip: You can use GetFileAttributes to retrieve the current attributes of a file before modifying them with SetFileAttributes. This can help in constructing the new attribute value without accidentally clearing desired existing attributes.
Warning: Modifying critical system file attributes can lead to system instability. Exercise caution when changing attributes of system or protected files.

File Attribute Flags

Flag Meaning
FILE_ATTRIBUTE_ARCHIVE The file or directory is an archive file. Applications use this to mark files for backup or removal.
FILE_ATTRIBUTE_COMPRESSED The file or directory is compressed. For a file, this means that the data in the file is compressed. For a directory, this means that compression is enabled for files and subdirectories created within the directory.
FILE_ATTRIBUTE_DIRECTORY The file is a directory.
FILE_ATTRIBUTE_ENCRYPTED The file or directory is encrypted using the Encrypting File System (EFS). For a decrypted file, this flag is zero. NULL is returned if the file is encrypted and the calling process does not have the required access permissions.
FILE_ATTRIBUTE_HIDDEN The file or directory is hidden. It is not included in an ordinary directory listing.
FILE_ATTRIBUTE_NORMAL This flag can be used with other flags. It is set if a file has none of the other attributes set. If this flag is set, other flags have no meaning.
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED The file or directory is not included in content index matching.
FILE_ATTRIBUTE_OFFLINE The data of the file is not immediately available. This flag indicates that the file data is queued for retrieval.
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_REPARSE_POINT A reparse point exists on the file or directory. This flag is used by the file system, not by the user.
FILE_ATTRIBUTE_SPARSE_FILE The file is a sparse file.
FILE_ATTRIBUTE_SYSTEM The file or directory is a system file.
FILE_ATTRIBUTE_TEMPORARY The file is being used as a temporary file. File systems try to keep as much data as possible in memory for temporary files. The system can delete temporary files at any time.

Example

The following code example demonstrates how to set a file to be read-only.

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

int main() {
    const char* fileName = "my_document.txt";
    DWORD attributes = FILE_ATTRIBUTE_READONLY;

    if (SetFileAttributes(fileName, attributes)) {
        std::cout << "Successfully set attributes for " << fileName << std::endl;
    } else {
        std::cerr << "Error setting attributes for " << fileName << ". Error code: " << GetLastError() << std::endl;
    }

    return 0;
}