GetFileAttributes
Retrieves the attributes of a specified file or directory.
Syntax
DWORD GetFileAttributes(
LPCSTR lpFileName
);
Parameters
-
lpFileName
LPCSTR
A pointer to a null-terminated string that specifies the name of the file or directory. This parameter can be a device name, a drive, a path, or a filename.
Return Value
If the function succeeds, the return value is the attribute flags for the specified file or directory. If the function fails, the return value is INVALID_FILE_ATTRIBUTES. To get extended error information, call GetLastError.
Remarks
The attribute flags are a combination of the following values:
| Attribute | Meaning |
|---|---|
FILE_ATTRIBUTE_ARCHIVE |
The file or directory is an archive file. Applications can use this to back up or delete files. |
FILE_ATTRIBUTE_COMPRESSED |
This attribute is reserved for future use. |
FILE_ATTRIBUTE_DEVICE |
This attribute is for a device. |
FILE_ATTRIBUTE_DIRECTORY |
The specified path is a directory. |
FILE_ATTRIBUTE_ENCRYPTED |
The 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 is reserved for future use. |
FILE_ATTRIBUTE_NORMAL |
This attribute has no meaning and can be used to combine other attributes. |
FILE_ATTRIBUTE_OFFLINE |
The data is not immediately available. This attribute indicates that the retrieved data is the stub of a non-resident file. |
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 |
The file or directory has a reparse point, which is a buffer that tags the file system with a specific metadata. |
FILE_ATTRIBUTE_SPARSE_FILE |
The file is a sparse file. |
FILE_ATTRIBUTE_SYSTEM |
The file or directory is a system file. The system uses this attribute to identify files that are necessary for the operating system to function. |
FILE_ATTRIBUTE_TEMPORARY |
The file is being used as a temporary file. File systems try to keep all data in memory in modified pages, for temporary files. |
FILE_ATTRIBUTE_UNCOMPRESSED_HINTS |
This attribute is reserved for future use. |
Example
#include <windows.h>
#include <stdio.h>
int main() {
DWORD attributes;
const char* fileName = "example.txt"; // Replace with your file name
attributes = GetFileAttributes(fileName);
if (attributes == INVALID_FILE_ATTRIBUTES) {
printf("Failed to get attributes for %s. Error code: %lu\n", fileName, GetLastError());
return 1;
}
printf("Attributes for %s:\n", fileName);
if (attributes & FILE_ATTRIBUTE_ARCHIVE) printf("- FILE_ATTRIBUTE_ARCHIVE\n");
if (attributes & FILE_ATTRIBUTE_DIRECTORY) printf("- FILE_ATTRIBUTE_DIRECTORY\n");
if (attributes & FILE_ATTRIBUTE_HIDDEN) printf("- FILE_ATTRIBUTE_HIDDEN\n");
if (attributes & FILE_ATTRIBUTE_READONLY) printf("- FILE_ATTRIBUTE_READONLY\n");
if (attributes & FILE_ATTRIBUTE_SYSTEM) printf("- FILE_ATTRIBUTE_SYSTEM\n");
if (attributes & FILE_ATTRIBUTE_TEMPORARY) printf("- FILE_ATTRIBUTE_TEMPORARY\n");
// Add checks for other attributes as needed
return 0;
}