删除当前目录n个月前的文件

来源:互联网 发布:excel如何拆分数据公式 编辑:程序博客网 时间:2024/06/07 20:22
#include <stdio.h>#include <sys/stat.h>#include <time.h>#include <dirent.h>#define MON 2678400int main(int argc, char *argv[]){DIR *dp;struct dirent *dirp;dp = opendir("./");struct stat stbuf;long now;time(&now);int m = 6;if (argc > 1)m = atoi(argv[1]);if (m == 0){printf("usage: clean months\n");return 0;}int total = 0;while ((dirp = readdir(dp)) != NULL){char *filename = dirp -> d_name;if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)continue;stat(filename, &stbuf);if (now - stbuf.st_mtime <= MON * m)continue;remove(filename);total++;}closedir(dp);printf("clean: remove %d files\n", total);return 0;}

0 0
原创粉丝点击