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

6 篇文章 0 订阅

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

#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文件.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值