获取文件创建或修改时间?

时间:2015-04-01 02:30:28

标签: c++ winapi mfc

我只想创建或修改文件的时间,并将其与系统时间进行比较。

if (fileExtensions[i] == restoken   &&  lmdCheck.GetCheck() == true)
{
    testbox3.AddString(allFiles[i]);
    CFileStatus status;
    CFile cfile;

    //cfile.Open(allFiles[i],NULL,NULL);
    cfile.Open(allFiles[i], CFile::modeRead | CFile::modeWrite);

    CTime t = CTime::GetCurrentTime();
    TRACE(t.Format(L"%X\n"));

    SYSTEMTIME SystemTime;
    //::GetSystemTime(&SystemTime);
    ::GetLocalTime(&SystemTime);
    CTime SystemT(SystemTime);
    TRACE(SystemT.Format(L"%X\n"));
    ULONGLONG dwNewLength = 10000;
    cfile.SetLength(dwNewLength);

    if (cfile.GetStatus(status))
    {
        if (status.m_ctime < SystemTime)
        {
            cfile.Close();
            CFile::Remove(allFiles[i]);
        }
    }
}

删除那些少于系统时间的文件。请帮帮忙?

2 个答案:

答案 0 :(得分:1)

对不起,我的错误。我不希望系统时间我想要数据和时间选择器数据。 我用这段代码解决了我的问题。

for (int i = 0; i < allFiles.GetSize(); i++)
    {
        GetFileExt(allFiles[i]);


        CString str, restoken;
        fileExt.GetWindowText(str);

        int curPos = 0;
        restoken = str.Tokenize(_T(" "), curPos);

        while (restoken != _T(""))
        {
            textBox2.AddString(restoken);
            int a = fileExtensions.GetSize();

            if (fileExtensions[i] == restoken   &&  lmdCheck.GetCheck() == true)
            {
                testbox3.AddString(allFiles[i]);
                CFileStatus status;
                CFile cfile;

                cfile.Open(allFiles[i], CFile::modeRead | CFile::modeWrite);

                if (cfile.GetStatus(status))
                {

                    CTime variable, clDate;
                    lmdDate.GetTime(clDate);

                    if (status.m_mtime < clDate)
                    {
                        cfile.Close();
                        CFile::Remove(allFiles[i]);
                    }
                }
            }

            restoken = str.Tokenize(_T(" "), curPos);
        }

再见谢谢你的朋友。

答案 1 :(得分:0)

这会将文件的FILETIME与当前的SYSTEMTIME

进行比较
BOOL get_finddata(const TCHAR *filename, WIN32_FIND_DATA &data)
{
    if (!filename) return FALSE;
    HANDLE h = FindFirstFile(filename, &data);
    if (h == INVALID_HANDLE_VALUE)
        return FALSE;
    FindClose(h);
    return TRUE;
}

void change_filetime(FILETIME &ft, int sec)
{
    ULONGLONG temp = (((ULONGLONG)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
    temp += sec * __int64(10000000);//100-nanosecond intervals
    ft.dwLowDateTime = (DWORD)(temp & 0xFFFFFFFF);
    ft.dwHighDateTime = (DWORD)(temp >> 32);
}

void checkfor_older_files(const TCHAR *filename)
{
    WIN32_FIND_DATA finddata;
    if (!get_finddata(filename, finddata)) return;
    //finddata.ftCreationTime;
    //finddata.ftLastWriteTime;
    //finddata.ftLastAccessTime;

    SYSTEMTIME st;
    GetSystemTime(&st);

    FILETIME comparetime;
    SystemTimeToFileTime(&st, &comparetime);

    //change comparetime, for example, get files older than 30 minutes
    change_filetime(comparetime, -30 * 60);

    if (CompareFileTime(&finddata.ftCreationTime, &comparetime) < 0)
        OutputDebugString(L"older file\n");
    else
        OutputDebugString(L"not old\n");
}