比较文件时间功能 [英] Compare File Time Function

查看:87
本文介绍了比较文件时间功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可能和我一样讨厌此代码,但是Albert Holguin告诉我可以做到这一点.他是这里最聪明的人之一.我已经走了这么远.我只需要完成它.我正在尝试删除少于一分钟的任何文件.我想我有关于systemtofile的第一部分,现在从我阅读的内容中我需要CompareFileTime.我的第一个问题是我不确定将其放置在何处,我们将不胜感激.谢谢.

I know your probably as sick of this code as I am, but I was told by Albert Holguin that this can be done. And he''s one of the brightest guys on here. I''ve gotten this far. I just need to finish it. I''m trying to delete any file that is less than a minute old. I think I have the first part about the systemtofile, now from what I read I need to ComparefileTime. My first problem is that I am not sure where to place it, any help is appreciated. Thank you.

struct file_data 
{ 
    std::wstring sLastAccessTime; 
    __int64 nFileSize      ; 
};

int GetFileList(const wchar_t *searchkey, std::map &map) 
{ 
    WIN32_FIND_DATA fd; 
    HANDLE h = FindFirstFile(searchkey,&fd); 
    if(h == INVALID_HANDLE_VALUE) 
    { 
        return 0; // no files found 
    } 
    while(1) 
    { 
       	wchar_t buf[128]; 
        SYSTEMTIME st;
		FILETIME ftNow;
		LARGE_INTEGER li;    
		GetSystemTime(&st);
		SystemTimeToFileTime(&st, &ftNow);
    	li.LowPart = ftNow.dwLowDateTime;
		li.HighPart = ftNow.dwHighDateTime;
		
		GetSystemTimeAsFileTime(&ftNow);
		auto ftAs64 = ftNow.dwLowDateTime + ((unsigned __int64)ftNow.dwHighDateTime << 32) - 6000000000UL;
		FILETIME ftOneMinuteAgo = { (DWORD)ftAs64, (DWORD)(ftAs64 >> 32) };
		FileTimeToSystemTime(&ftNow, &st); 
        wsprintf(buf, L"%d-%02d-%02d",st.wYear, st.wMonth, st.wDay); 
		
        file_data filedata; 
        filedata.sLastAccessTime= buf; 
        filedata.nFileSize      = (((__int64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow; 
 
        map[fd.cFileName]= filedata; 
		//////////////////////////////////////////////////////
        if (FindNextFile(h, &fd) == FALSE)
			GetFileTime(&ftOneMinuteAgo);
		if (-1 == CompareFileTime (&ftMostRecent, &ft))
		{
		// ... do something ... //
		}
		//////////////////////////////////////////////////////
            break; 
    } 
     FindClose(h);
	return map.size(); 
} 
 
int main() 
{ 
	std::map map; 
    GetFileList(L"C:\\Mapper\\*.txt", map);
	GetFileList(L"C:\\Mapper\\*.pdf", map);
	GetFileList(L"C:\\Mapper\\*.jpg", map);
	{
		if (map.size() > 0) 
		{ 
		printf("\n   Delete: %i   \n", map.size());
		} 
		else 
		{ 
		printf ("%s \n", "Nothing");
		}
	system("pause");
    return 0;
}
}

推荐答案

将其放在需要的地方!您是有要求的人,因此您必须确定在哪里进行此测试.您是要在找到文件时检查文件,还是在决定删除文件还是在程序中的其他位置检查文件?

就个人而言,我将在我要寻找删除候选对象的部分代码中使用此测试.如果文件符合搜索条件并且不到1分钟,则将其添加到列表中,然后可以通过删除功能对其进行处理.
Place it where it is needed! You are the one with the requirements so you must decide where you need to make this test. Do you want to check it at the time you find a file, at the time you need to decide whether to delete it or at some other point in your program?

Personally, I would use this test during the part of the code where I was looking for candidates for deletion. If the file matches the search criteria and is less than 1 minute old, add it to a list which can then be processed by the delete function.


在上面的讨论中,我忍不住想知道为什么要为此任务使用地图?由于FindFirstFile和FindNextFile在单个块中为您提供了您想要的所有数据(以及更多),因此我将使用向量将其存储.

玩具:

Further to the discussion above, I can''t help but wonder why use a map for this task? Since FindFirstFile and FindNextFile give you all the data you want (and more) in a single chunk, I''d just store that using a vector.

Something to toy with:

#include <iostream>
#include <string>
#include <vector>
#include <windows.h>

using namespace std;

typedef vector<WIN32_FIND_DATA> tFoundFilesVector;

int getFileList(string filespec, tFoundFilesVector &foundFiles)
{
    WIN32_FIND_DATA findData;
    HANDLE h;
    bool validResult=true;
    int numFoundFiles = 0;

    h = FindFirstFile(filespec.c_str(), &findData);

    if (h == INVALID_HANDLE_VALUE)
        return 0;

    while (validResult)
    {
        numFoundFiles++;
        foundFiles.push_back(findData);
        validResult = FindNextFile(h, &findData);
    }
    return numFoundFiles;
}

void showFileAge(tFoundFilesVector &fileList)
{
    INT64 fileTime, curTime, age;
    tFoundFilesVector::iterator iter;

    CoFileTimeNow(&ftNow);
    curTime = ((unsigned INT64) ftNow.dwHighDateTime << 32) + ftNow.dwLowDateTime;

    for (iter=fileList.begin(); iter<fileList.end(); iter++)
    {
        fileTime = ((unsigned INT64)iter->ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime;

        age = curTime - fileTime;

        cout << "FILE: ''" << iter->cFileName << "'', AGE: " << (INT64)age/10000000UL << " seconds" << endl;
    }
}

int main()
{
    string fileSpec = "*.*";
    tFoundFilesVector foundFiles;
    tFoundFilesVector::iterator iter;

    int foundCount = 0;

    getFileList("*.c??", foundFiles);
    getFileList("*.h", foundFiles);

    foundCount = foundFiles.size();
    if (foundCount)
    {
        cout << "Found "<<foundCount<<" matching files.\n";
        showFileAge(foundFiles);
    }
    return 0;
}


这篇关于比较文件时间功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆