注: 文章内容来源于网络:
#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文件.