linux删除文件夹下所有内容
2014-06-26 15:30
811 查看
linux 下面 删除文件夹下的所有内容,忘了是在哪里找到的原型,自己又改了改,现在是完全可以用的。
//判断是否为目录
bool is_dir(const char *path)
{
struct statstatbuf;
if(lstat(path,&statbuf) ==0)
{
return S_ISDIR(statbuf.st_mode) != 0;
}
return false;
}
//判断是否为常规文件
bool is_file(const char *path)
{
struct statstatbuf;
if(lstat(path,&statbuf) ==0)
return S_ISREG(statbuf.st_mode) != 0;
return false;
}
//判断是否是特殊目录
bool is_special_dir(const char *path)
{
return strcmp(path, ".")== 0 || strcmp(path, "..") == 0;
}
//生成完整的文件路径
void get_file_path(const char *path, const char *file_name, char *file_path)
{
strcpy(file_path,path);
if(file_path[strlen(path) - 1] != '/')
strcat(file_path, "/");
strcat(file_path,file_name);
}
void delete_file(const char *path)
{
DIR *dir;
dirent *dir_info;
char file_path[PATH_MAX];
if(is_file(path))
{
remove(path);
return;
}
if(is_dir(path))
{
if((dir = opendir(path)) ==NULL)
return;
while((dir_info =readdir(dir)) != NULL)
{
if(is_special_dir(dir_info->d_name))
continue;
get_file_path(path, dir_info->d_name, file_path);
if(is_dir(file_path))
{
delete_file(file_path);
rmdir(file_path);
}
elseif(is_file(file_path))
{
remove(file_path);
}
}
closedir(dir);//关闭目录
}
}
//判断是否为目录
bool is_dir(const char *path)
{
struct statstatbuf;
if(lstat(path,&statbuf) ==0)
{
return S_ISDIR(statbuf.st_mode) != 0;
}
return false;
}
//判断是否为常规文件
bool is_file(const char *path)
{
struct statstatbuf;
if(lstat(path,&statbuf) ==0)
return S_ISREG(statbuf.st_mode) != 0;
return false;
}
//判断是否是特殊目录
bool is_special_dir(const char *path)
{
return strcmp(path, ".")== 0 || strcmp(path, "..") == 0;
}
//生成完整的文件路径
void get_file_path(const char *path, const char *file_name, char *file_path)
{
strcpy(file_path,path);
if(file_path[strlen(path) - 1] != '/')
strcat(file_path, "/");
strcat(file_path,file_name);
}
void delete_file(const char *path)
{
DIR *dir;
dirent *dir_info;
char file_path[PATH_MAX];
if(is_file(path))
{
remove(path);
return;
}
if(is_dir(path))
{
if((dir = opendir(path)) ==NULL)
return;
while((dir_info =readdir(dir)) != NULL)
{
if(is_special_dir(dir_info->d_name))
continue;
get_file_path(path, dir_info->d_name, file_path);
if(is_dir(file_path))
{
delete_file(file_path);
rmdir(file_path);
}
elseif(is_file(file_path))
{
remove(file_path);
}
}
closedir(dir);//关闭目录
}
}
相关文章推荐
- 复制文件夹所有内容 和 删除整个文件夹的2个函数(原创)
- VC 删除文件夹 及 下面所有内容
- 递归删除一个文件夹中的所有内容
- JAVA处理文件(新建目录,新建文件,删除文件,删除文件夹,删除文件夹里面的所有文件,复制单个文件复制整个文件夹内容)
- Python_删除/创建指定目录及其下所有子目录的文件,该文件记录当前文件夹的内容
- 复制和删除文件夹所有内容
- asp.net C# 中删除一个文件夹里的所有内容
- Java新建目录、新建文件、删除文件、删除文件夹、删除文件夹里面的所有文件、复制整个文件夹内容
- 删除文件夹及文件夹内部所有内容的标准函数
- linux下删除文件夹内的指定内容
- 复制文件夹所有内容 和 删除整个文件夹的2个函数
- 递归调用删除文件夹及文件夹下所有内容
- fso删除当前文件夹下所有的内容
- Linux下PHP删除一个目录下所有文件夹和子目录
- linux命令删除当前目录即其子目录下的所有文件夹
- 复制文件夹所有内容 和 删除整个文件夹的2个函数
- 复制文件夹所有内容 和 删除整个文件夹的2个函数
- 如何批量删除指定路径中的所有内容为空的文件夹
- 递归 删除 某个文件夹 下所有的内容,包括子文件夹
- Linux下PHP删除一个目录下所有文件夹和子目录