Windows平台下C语言获取文件夹创建日期的问题求助(文件正常文件夹无效)
Hey there, I see you've got file creation dates working but folders are giving you trouble—let's sort this out! On Windows, the core Win32 APIs handle both files and folders, but you need to adjust how you target folders and access their metadata correctly.
核心原理
Windows stores folder creation timestamps in the same way as files, but you need to:
- Correctly identify or open the folder (using flags that support directory access)
- Extract the
ftCreationTimefield from the relevant API structure - Convert the raw
FILETIMEvalue to a human-readable format
示例1:获取单个指定文件夹的创建日期
This code uses FindFirstFile (which works great for folders) to pull the creation timestamp directly:
#include <windows.h> #include <stdio.h> void GetFolderCreationDate(const char* folderPath) { WIN32_FIND_DATA findData; HANDLE hFind = FindFirstFile(folderPath, &findData); if (hFind == INVALID_HANDLE_VALUE) { printf("Error accessing folder: %lu\n", GetLastError()); return; } // Check if we're actually dealing with a folder if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { printf("Path is not a folder!\n"); FindClose(hFind); return; } // Convert FILETIME to local system time FILETIME localFileTime; SYSTEMTIME systemTime; FileTimeToLocalFileTime(&findData.ftCreationTime, &localFileTime); FileTimeToSystemTime(&localFileTime, &systemTime); printf("Folder creation date: %04d-%02d-%02d %02d:%02d:%02d\n", systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute, systemTime.wSecond); FindClose(hFind); } int main() { const char* targetFolder = "C:\\Your\\Target\\Folder"; GetFolderCreationDate(targetFolder); return 0; }
示例2:遍历目录时获取所有文件夹的创建日期
If you're scanning a directory and want to grab creation dates for all subfolders, adjust your existing loop to check for the FILE_ATTRIBUTE_DIRECTORY flag:
#include <windows.h> #include <stdio.h> #include <string.h> void ScanDirectoryFolders(const char* dirPath) { char searchPath[MAX_PATH]; snprintf(searchPath, MAX_PATH, "%s\\*", dirPath); WIN32_FIND_DATA findData; HANDLE hFind = FindFirstFile(searchPath, &findData); if (hFind == INVALID_HANDLE_VALUE) { printf("Error scanning directory: %lu\n", GetLastError()); return; } do { // Skip "." and ".." directories if (strcmp(findData.cFileName, ".") == 0 || strcmp(findData.cFileName, "..") == 0) { continue; } // Check if this is a folder if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { char fullPath[MAX_PATH]; snprintf(fullPath, MAX_PATH, "%s\\%s", dirPath, findData.cFileName); FILETIME localFileTime; SYSTEMTIME systemTime; FileTimeToLocalFileTime(&findData.ftCreationTime, &localFileTime); FileTimeToSystemTime(&localFileTime, &systemTime); printf("Folder: %-20s Created: %04d-%02d-%02d %02d:%02d:%02d\n", findData.cFileName, systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute, systemTime.wSecond); } } while (FindNextFile(hFind, &findData) != 0); FindClose(hFind); } int main() { const char* targetDir = "C:\\Your\\Parent\\Directory"; ScanDirectoryFolders(targetDir); return 0; }
常见问题排查
If your original code didn't work for folders, it's likely one of these issues:
- You were only checking for
FILE_ATTRIBUTE_NORMAL(which applies to files, not folders) - When using
CreateFileto open a folder, you forgot theFILE_FLAG_BACKUP_SEMANTICSflag (required to open directories as handles) - You skipped over "." and ".." but didn't handle other subfolders correctly
内容的提问来源于stack exchange,提问作者Salvador C

