按时间戳删除文件 [英] Delete a File by Timestamp

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

问题描述

再来一次…….我正在尝试删除一个不到1分钟的文件.我不知道它的名字或位置.我只想删除zip txt或jpg.我唯一知道的另一件事就是时间戳.有人告诉我,某些非常聪明的人可以做到这一点.我找不到也不知道.任何帮助表示赞赏.我当前正在使用一个批处理文件,我想更改它并使用直接C ++.请有人帮我,这样我就可以把这只小狗睡觉了!!!!谢谢.

Here we go......again. I''m trying to delete a file that is less than 1 minute old. I don''t know it''s name or location. I want to only delete a zip txt or jpg. The only other thing I know is the timestamp. I''ve been told that this is possible by some really smart people, however; I can''t find nor figure it out. ANY help is appreciated. I am currently using a batch file, I would like to change that and go with straight C++. Please someone help me so I can put this puppy to bed!!!! Thank you.

#include <string>
#include <vector>
#include <windows.h>
#include <time.h>
#include <map>
 

int GetFileList(const wchar_t *searchkey, std::map<std::wstring,> &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];
     FILETIME ft = fd.ftLastWriteTime;
     SYSTEMTIME sysTime;
     FileTimeToSystemTime(&ft, &sysTime);
     wsprintf(buf, L"%d-%02d-%02d",sysTime.wYear, sysTime.wMonth, sysTime.wDay);
     map[fd.cFileName] = buf;
        if(FindNextFile(h, &fd) == FALSE)
            break;
    }
    return map.size();
}
 
void main()
{ 
    std::map<std::wstring,> map;
    int count = GetFileList(L"*.*", map);
    // Using const_iterator
    for(std::map<std::wstring,>::const_iterator it = map.begin(); 
      it != map.end(); ++it)
    {
       MessageBoxW(NULL,it->first.c_str(),L"One",MB_OK);
       MessageBoxW(NULL,it->second.c_str(),L"Two",MB_OK);
    }
} 
 </time.h></windows.h></vector></string>



所以....现在我应该在哪里添加文件类型,并且一分钟以内的时间更少?并作为目录参考或驱动器以供将来参考?谢谢.



So....now where do I add the file types and the time of less thn one minute? And for future reference the directory(s) or drive to search? Thank you.

推荐答案

该问题在其他地方已得到完全解答.使用 FindFirstFile/FindNextFile [
This question has been answered fully elsewhere. Use the FindFirstFile/FindNextFile[^] functions to search for files and then delete the ones that match your search criteria. Incidentally, this is exactly what the shell will do when it runs your complicated batch file.


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

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