Linux下递归遍历指定目录下的文件内容并删除的函数实现
2016-06-11 22:38
946 查看
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
int is_dir(const char *path) // 判断是否为目录
{
struct stat statbuf;
if(stat(path, &statbuf) == 0) //将path文件名信息写入stat结构体
{
return S_ISDIR(statbuf.st_mode); //返回是否为目录
}
return 0;
}
int is_file(const char *path) //判断是否为文件
{
struct stat statbuf;
if(stat(path, &statbuf) == 0) //将path文件名信息写入stat结构体
{
return S_ISREG(statbuf.st_mode); //返回是否为文件
}
return 0;
}
int is_special_file(const char *path) //判断是否为“.”或".."
{
return (strcmp(path, ".") == 0 || strcmp(path, "..") == 0);
}
void get_file_path(const char *path, const char *filename, char *file_path) //组合路径和当前目录名,成为新路径:file_path
{
strcpy(file_path, path);
if(file_path[strlen(file_path) - 1] != '/')
{
strcat(file_path, "/");
}
strcat(file_path, filename);
return;
}
void delete_file(const char *path)
{
DIR *dir;
struct dirent *dir_info;
char file_path[100];
if(is_file(path)) //判断是否为文件
{
printf("delete:%s\n", path);
remove(path);
return;
}
if(is_dir(path)) // 判断是否为目录
{
if((dir = opendir(path)) == NULL) //目录打开失败返回
{
return;
}
while(dir_info = readdir(dir))
{
if(is_special_file(dir_info->d_name)) //判断是否为“.”或".."
{
continue;
}
get_file_path(path,dir_info->d_name, file_path); //组合路径和当前目录名,成为新路径:file_path
delete_file(file_path);
}
rmdir(path); //删除空目录
printf("rmdir: %s\n", path);
}
}
int main()
{
delete_file("./aaa");
return 0;
}
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
int is_dir(const char *path) // 判断是否为目录
{
struct stat statbuf;
if(stat(path, &statbuf) == 0) //将path文件名信息写入stat结构体
{
return S_ISDIR(statbuf.st_mode); //返回是否为目录
}
return 0;
}
int is_file(const char *path) //判断是否为文件
{
struct stat statbuf;
if(stat(path, &statbuf) == 0) //将path文件名信息写入stat结构体
{
return S_ISREG(statbuf.st_mode); //返回是否为文件
}
return 0;
}
int is_special_file(const char *path) //判断是否为“.”或".."
{
return (strcmp(path, ".") == 0 || strcmp(path, "..") == 0);
}
void get_file_path(const char *path, const char *filename, char *file_path) //组合路径和当前目录名,成为新路径:file_path
{
strcpy(file_path, path);
if(file_path[strlen(file_path) - 1] != '/')
{
strcat(file_path, "/");
}
strcat(file_path, filename);
return;
}
void delete_file(const char *path)
{
DIR *dir;
struct dirent *dir_info;
char file_path[100];
if(is_file(path)) //判断是否为文件
{
printf("delete:%s\n", path);
remove(path);
return;
}
if(is_dir(path)) // 判断是否为目录
{
if((dir = opendir(path)) == NULL) //目录打开失败返回
{
return;
}
while(dir_info = readdir(dir))
{
if(is_special_file(dir_info->d_name)) //判断是否为“.”或".."
{
continue;
}
get_file_path(path,dir_info->d_name, file_path); //组合路径和当前目录名,成为新路径:file_path
delete_file(file_path);
}
rmdir(path); //删除空目录
printf("rmdir: %s\n", path);
}
}
int main()
{
delete_file("./aaa");
return 0;
}
相关文章推荐
- linux下fork()函数讲解
- linux中内存使用,swap,cache,buffer的含义总结
- linux dd命令
- Linux中程序的栈帧分析以及修改函数地址
- Linux系统信息
- linux 系统调用sysconf函数使用
- 聊聊Linux动态链接中的PLT和GOT(3)——公共GOT表项
- Linux 基础知识(三)
- linux 切换c++版本
- 1、Linux文本处理基础命令
- ubuntu-14.04.3-server-amd64下源码安装mysql-5.6.27-linux-glibc2.5-x86_64
- 权限管理[Linux]
- 文件管理[Linux]
- Centos中部署WordPress无法更新问题
- Linux下定时执行任务的方法
- Linux系统修改编码
- Linux进程同步之POSIX信号量(非原创)
- informix数据库Linux/Unix下安装配置
- Linux常用命令
- Linux下安装配置JDK