linux C语言实现递归删除文件文件夹功能
2015-08-31 19:49
836 查看
static int remove_dir(const char *dirname)
{
DIR *dir;
struct dirent *entry;
char path[PATH_MAX];
dir = opendir(dirname);
if (dir == NULL) {
LOGE("opendir %s failed\n", dirname);
return -1;
}
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
snprintf(path, (size_t) PATH_MAX, "%s/%s", dirname, entry->d_name);
if (entry->d_type == DT_DIR) {
remove_dir(path);
} else {
// delete file
unlink(path);
}
}
}
closedir(dir);
// now we can delete the empty dir
rmdir(dirname);
return 0;
}
{
DIR *dir;
struct dirent *entry;
char path[PATH_MAX];
dir = opendir(dirname);
if (dir == NULL) {
LOGE("opendir %s failed\n", dirname);
return -1;
}
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
snprintf(path, (size_t) PATH_MAX, "%s/%s", dirname, entry->d_name);
if (entry->d_type == DT_DIR) {
remove_dir(path);
} else {
// delete file
unlink(path);
}
}
}
closedir(dir);
// now we can delete the empty dir
rmdir(dirname);
return 0;
}
相关文章推荐
- Linux学习笔记--------“./a.out”是什么意思???
- Linux学习笔记------如何执行一个 可执行程序???
- 28个Unix/Linux的命令行神器
- 应该知道的Linux技巧
- Linux安装
- linux下 常用命令
- PHP在linux上执行外部命令
- linux基本命令
- Linux NAT Router 桥作为WAN,桥下接口作为LAN如何使用
- linux运维实战练习-2015年8月27日课程作业
- linux下ppp的移植及ppp0路由设置
- Linux给用户添加sudo权限
- Linux运维工程师入门须掌握的10个技术点
- Linux下使用nexus搭建maven私服
- CentOS 7 下 ifconfig command not found 解决办法
- 详解Linux内核之双向循环链表
- linux sh脚本启动异常/bin/sh^M: bad interpreter: No such file or directory
- Linux 之安装文件
- Linux Back Light driver
- CentOS7下安装JDK1.7