当前位置: 技术问答>linux和unix
linux 下怎样 定时 删除目录中的 指定的文件
来源: 互联网 发布时间:2016-04-12
本文导语: 各位哥哥姐姐: 小弟有个问题请教:一个程序每天都会产生一个*.txt文件(日志),*为这种格式:20080905charstring,也就是年月日加一些字符,现在的问题是:在linux中用c语言实现这个功能:定时的删除保存时间...
各位哥哥姐姐:
小弟有个问题请教:一个程序每天都会产生一个*.txt文件(日志),*为这种格式:20080905charstring,也就是年月日加一些字符,现在的问题是:在linux中用c语言实现这个功能:定时的删除保存时间超过2周的*.txt文件。
注:*.txt文件保存的路径是不同的,需要配置路径。
谢谢谢谢了!!!!!!
小弟有个问题请教:一个程序每天都会产生一个*.txt文件(日志),*为这种格式:20080905charstring,也就是年月日加一些字符,现在的问题是:在linux中用c语言实现这个功能:定时的删除保存时间超过2周的*.txt文件。
注:*.txt文件保存的路径是不同的,需要配置路径。
谢谢谢谢了!!!!!!
|
#include
#include
#include
#include
#include
#include
int main()
{
DIR *db;
char filename[256],path[1024];
struct dirent *p;
FILE *file;
time_t curdate,towwork;
struct tm *tdate;
int year=0,month=0,day=0;
file=fopen("./config","r");
if(!file){
printf("config not exist!n");
return -1;
}
memset(path,0,sizeof(path));
fgets(path,sizeof(path),file);
if(path[strlen(path)-1]=='n')path[strlen(path)-1]='';
db=opendir(path);
if(!db){
printf("open %s error!n",path);
return -1;
}
while(p=readdir(db))
{
if(strcmp(p->d_name,".")==0||strcmp(p->d_name,"..")==0)
continue;
time(&curdate);
tdate = localtime (&curdate);
year=day=month=0;
sscanf(p->d_name,"%04d%02d%02d",&year,&month,&day);
if(year==0||month==0||day==0)continue;
tdate->tm_mday = day;
tdate->tm_mon = month-1;
tdate->tm_year =year-1900;
towwork=mktime(tdate);
if(towworkd_name);
}
closedir(db);
}
config
./
#include
#include
#include
#include
#include
int main()
{
DIR *db;
char filename[256],path[1024];
struct dirent *p;
FILE *file;
time_t curdate,towwork;
struct tm *tdate;
int year=0,month=0,day=0;
file=fopen("./config","r");
if(!file){
printf("config not exist!n");
return -1;
}
memset(path,0,sizeof(path));
fgets(path,sizeof(path),file);
if(path[strlen(path)-1]=='n')path[strlen(path)-1]='';
db=opendir(path);
if(!db){
printf("open %s error!n",path);
return -1;
}
while(p=readdir(db))
{
if(strcmp(p->d_name,".")==0||strcmp(p->d_name,"..")==0)
continue;
time(&curdate);
tdate = localtime (&curdate);
year=day=month=0;
sscanf(p->d_name,"%04d%02d%02d",&year,&month,&day);
if(year==0||month==0||day==0)continue;
tdate->tm_mday = day;
tdate->tm_mon = month-1;
tdate->tm_year =year-1900;
towwork=mktime(tdate);
if(towworkd_name);
}
closedir(db);
}
config
./
|
“定时的删除保存时间超过2周的*.txt文件”
从上述功能的表述来看,我理解你是准备用一个常驻进程来定时处理,这样的话应该会用到daemon守护进程,可以把1楼的代码转化成daemon进程来处理。
如果是每天手动执行处理,那我觉得用不如shell来做,要简单的多。
从上述功能的表述来看,我理解你是准备用一个常驻进程来定时处理,这样的话应该会用到daemon守护进程,可以把1楼的代码转化成daemon进程来处理。
如果是每天手动执行处理,那我觉得用不如shell来做,要简单的多。
|
你说的都是windows下的格式,linux下不是以后缀来判断文件属性的