首页 > 代码库 > linux下的c语言编程删除文件夹
linux下的c语言编程删除文件夹
刚刚在学习开始做新项目的时候,学长布置了一项任务,就是在给定一个目录下,要将这个目录下一个星期之前的目录包括里面的文件全部删除,只保留这一个星期内的。百度了好久的资料,终于完成,记录一下防止忘记。(注:文件夹名称默认为日期格式,如20140716)
#include<dirent.h>
#include<sys/types.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<time.h>
int date;//已数字的形式存储当天的日期
int change(int data)//将文件夹名称转化为数字后加上7天后的日期
{
int newdata=http://www.mamicode.com/data;
switch((newdata/100)%100)
{
case 1:
{
if(((newdata%100)+7)<=31) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%31+100+newdata-(newdata%100);
break;
}
case 2:
{
if(((newdata/10000)%4==0&&(newdata/10000)%100!=0)||
((newdata/10000)%400==0))
{
if(((newdata%100)+7)<=28) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%28+100+newdata-(newdata%100);
}
else
{
if(((newdata%100)+7)<=29) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%29+100+newdata-(newdata%100);
}
break;
}
case 3:
{
if(((newdata%100)+7)<=31) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%31+100+newdata-(newdata%100);
break;
}
case 4:
{
if(((newdata%100)+7)<=30) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%30+100+newdata-(newdata%100);
break;
}
case 5:
{
if(((newdata%100)+7)<=31) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%31+100+newdata-(newdata%100);
break;
}
case 6:
{
if(((newdata%100)+7)<=30) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%30+100+newdata-(newdata%100);
break;
}
case 7:
{
if(((newdata%100)+7)<=31) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%31+100+newdata-(newdata%100);
break;
}
case 8:
{
if(((newdata%100)+7)<=31) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%31+100+newdata-(newdata%100);
break;
}
case 9:
{
if(((newdata%100)+7)<=30) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%30+100+newdata-(newdata%100);
break;
}
case 10:
{
if(((newdata%100)+7)<=31) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%31+100+newdata-(newdata%100);
break;
}
case 11:
{
if(((newdata%100)+7)<=30) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%30+100+newdata-(newdata%100);
break;
}
case 12:
{
if(((newdata%100)+7)<=31) newdata+=7;
else
newdata=http://www.mamicode.com/(newdata%100+7)%31+newdata-(newdata%10000)+10000;
break;
}
default:break;
}
return newdata;
}
void getdate()//获取当天的日期
{
time_t timep;
struct tm*p_tm;
timep=time(NULL);
p_tm=gmtime(&timep);
date=(p_tm->tm_year+1900)*10000+(p_tm->tm_mon+1)*100+p_tm->tm_mday;
}
void del_dir(char *npath)//删除给定一个文件夹里面所有的文件或者文件夹
{
char *pathname=(char *)malloc(256);
struct dirent* ent=NULL;
DIR *ptr;
DIR *dpin;
ptr=opendir(npath);
while((ent=readdir(ptr))!=NULL)
{
if(strcmp(ent->d_name,".")==0||strcmp(ent->d_name,"..")==0)
continue;
strcpy(pathname,npath);
strcpy(pathname,"\/");//这个地方是一个‘\‘和一个‘/‘;
strcpy(pathname,ent->d_name);
if(ent->d_type==8)
{
remove(pathname);
}
else
{
del_dir(pathname);//由于remove()只能删除空文件夹,所以此处用了递归删除
remove(pathname);
}
}
}
void readirect(char *path)//遍历所有文件夹并找出一个礼拜前的文件夹进行删除
{
char *pathname=(char *)malloc(256);
struct dirent* ent = NULL;
DIR *ptr;
DIR *dpin;
ptr=opendir(path);
while((ent=readdir(ptr)) != NULL)
{
if(strcmp(ent->d_name,".")==0||strcmp(ent->d_name,"..")==0)
continue;
strcpy(pathname,path);
strcat(pathname,"\/");
strcat(pathname,ent->d_name);
if(change(atoi(ent->d_name))<date)
{
del_dir(pathname);
remove(pathname);
}
}
ptr = NULL:
}
int main(int argc,char *argv[])
{
getdate();
readirect(argv[1]);
return 0;
}