Windows 中删除某一文件夹下某一时间前创建的文件.

来源:互联网 发布:涡扇15和f119知乎 编辑:程序博客网 时间:2024/05/22 08:20

注:  文章内容来源于网络: 

#include <sys/stat.h> 
void DelteOldFiles(){char path[256];GetModuleFileNameA(NULL,path,sizeof(path));std::string strFullPath =path;std::string curPath = strFullPath.substr(0,strFullPath.rfind("\\")+1);std::string strSourceDir= (curPath+"Log\\*.*").c_str();std::string strPrefix  = curPath+"Log\\";const int SECONDS_OF_DAY= 86400;time_t curSecond = time(0); struct _stat buf;_finddata_t file; long longf; if((longf = _findfirst(strSourceDir.c_str(), &file))==-1l) { return ;} else { std::string tempName; while( _findnext(longf, &file ) == 0) { tempName = ""; tempName = file.name; int result = _stat((strPrefix+tempName).c_str(),&buf);int createSecond = buf.st_atime;int span = curSecond - createSecond;/*删除 10天前创建的文件*/if (span>SECONDS_OF_DAY*10 && (tempName != "."||tempName !="..")){DeleteFileA((strPrefix+tempName).c_str());printf("this is old file\n");}if (tempName == "..") { continue; } }} _findclose(longf); }


这个实现的目标是 删除程序exe目录下log文件夹中10天前的文件,也就是比较老的log文件.


0 0
原创粉丝点击