You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows平台下C语言获取文件夹创建日期的问题求助(文件正常文件夹无效)

获取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:

  1. Correctly identify or open the folder (using flags that support directory access)
  2. Extract the ftCreationTime field from the relevant API structure
  3. Convert the raw FILETIME value 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 CreateFile to open a folder, you forgot the FILE_FLAG_BACKUP_SEMANTICS flag (required to open directories as handles)
  • You skipped over "." and ".." but didn't handle other subfolders correctly

内容的提问来源于stack exchange,提问作者Salvador C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:18:28