Function
DWORD GetFileAttributesW(LPCWSTR lpFileName);
This function is part of the Win32 API and is used to query file system information.
Parameters
lpFileName
[in] LPCWSTR lpFileName
The name of the file or directory. For more information, see Remarks.
Return Value
If the function succeeds, the return value is the attribute bits for the specified file or directory. If the function fails, the return value is INVALID_FILE_ATTRIBUTES (0xFFFFFFFF). To get extended error information, call GetLastError.
The return value is a bitmask that can be a combination of the following attributes:
| Attribute | 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 |
This attribute is supported for file-based compression on the NTFS file system. |
FILE_ATTRIBUTE_DIRECTORY |
The specified path is a directory. |
FILE_ATTRIBUTE_ENCRYPTED |
This attribute indicates that a file or directory is encrypted using the Encrypting File System (EFS). |
FILE_ATTRIBUTE_HIDDEN |
The file or directory is hidden. It is not included in an ordinary directory listing. |
FILE_ATTRIBUTE_INTEGRITY_STREAM |
This attribute applies to file system integrity. |
FILE_ATTRIBUTE_NORMAL |
This attribute has no meaning and cannot be assigned. |
FILE_ATTRIBUTE_OFFLINE |
The data is not available without accessing the remote storage. |
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_RECALL_ON_DATA_ACCESS |
This attribute indicates that the data is required to be recalled from remote storage. |
FILE_ATTRIBUTE_RECALL_ON_OPEN |
This attribute indicates that the file is not yet present locally. |
FILE_ATTRIBUTE_SPARSE_FILE |
The file is a sparse file. Sparse files save disk space by writing only the defined portions of the file. |
FILE_ATTRIBUTE_SYSTEM |
The file or directory is a system file. Files that are hidden and read-only are system files. |
FILE_ATTRIBUTE_TEMPORARY |
The file is being used as a temporary file. File systems try to avoid flushing it to disk. In general, this attribute will only be provided by file systems that are aware of the semantics of temporary files. |
FILE_ATTRIBUTE_UNCOMPRESSED_HINTS |
This attribute is supported for file-based compression on the NTFS file system. |
Remarks
To get the attributes for a file, pass the file name to the GetFileAttributesW function. If the file is in a different directory, specify the full path name.
lpFileName can be a file name or a directory name.
You can use the bitwise OR operator to combine attributes.
FILE_ATTRIBUTE_COMPRESSED, FILE_ATTRIBUTE_ENCRYPTED, or FILE_ATTRIBUTE_SPARSE_FILE.
Example
The following example retrieves the attributes for a file named "MyDocument.txt" and checks if it is read-only.
#include <windows.h>
#include <iostream>
int main() {
LPCWSTR fileName = L"MyDocument.txt";
DWORD attributes = GetFileAttributesW(fileName);
if (attributes == INVALID_FILE_ATTRIBUTES) {
std::cerr << "Failed to get file attributes. Error: " << GetLastError() << std::endl;
return 1;
}
if (attributes & FILE_ATTRIBUTE_READONLY) {
std::wcout << L"The file '" << fileName << L"' is read-only." << std::endl;
} else {
std::wcout << L"The file '" << fileName << L"' is not read-only." << std::endl;
}
if (attributes & FILE_ATTRIBUTE_DIRECTORY) {
std::wcout << L"The path '" << fileName << L"' is a directory." << std::endl;
} else {
std::wcout << L"The path '" << fileName << L"' is a file." << std::endl;
}
return 0;
}