C 语言递归删除目录和目录下的文件

7 篇文章 0 订阅

编程的时候经常会遇到需要删除指定目录和里面的文件目录的情况,这里献上我经常使用的代码

#ifdef WIN32
//判断是否是".."目录和"."目录
inline bool is_special_dir(const char *path)
{
    return strcmp(path, "..") == 0 || strcmp(path, ".") == 0;
}

//判断文件属性是目录还是文件
inline bool is_dir(int attrib)
{
    return attrib == 16 || attrib == 18 || attrib == 20;
}

//显示删除失败原因
inline void show_error(const char *file_name = NULL)
{
    errno_t err;
    _get_errno(&err);
    switch(err)
    {
    case ENOTEMPTY:
        LOGE<<"Given path is not a directory, the directory is not empty, or the directory is either the current working directory or the root directory";
        break;
    case ENOENT:
        LOGE<<"Path is invalid";
        break;
    case EACCES:
        LOGE<<file_name<<" had been opend by some application, can't delete";
        break;
    }
}

void get_file_path(const char *path, const char *file_name, char *file_path)
{
    strcpy(file_path, path);
    file_path[strlen(file_path) - 1] = '\0';
    strcat(file_path, file_name);
    strcat(file_path, "\\*");
}

//删除文件夹
void GF_DeleteDir(char *szDirPath)
{
    char szDir[255];
    strcpy(szDir,szDirPath);
    strcat(szDir,"\\*");

    GF_Delete_file(szDir);
    szDir[strlen(szDir) - 2] = '\0';
    if(_rmdir(szDir) == -1)
    {
        show_error();//目录非空则会显示出错原因
    }
}

//递归搜索目录中文件并删除
void GF_Delete_file(char *path)
{

    _finddata_t dir_info;
    _finddata_t file_info;
    intptr_t f_handle;
    char tmp_path[_MAX_PATH];
    if((f_handle = _findfirst(path, &dir_info)) != -1)
    {
        while(_findnext(f_handle, &file_info) == 0)
        {
            if(is_special_dir(file_info.name))
                continue;
            if(is_dir(file_info.attrib))//如果是目录,生成完整的路径
            {
                get_file_path(path, file_info.name, tmp_path);
                GF_Delete_file(tmp_path); //开始递归删除目录中的内容
                tmp_path[strlen(tmp_path) - 2] = '\0';
                if(file_info.attrib == 20)
                    LOGE<<"This is system file, can't delete!";
                else
                {
                    //删除空目录,必须在递归返回前调用_findclose,否则无法删除目录
                    if(_rmdir(tmp_path) == -1)
                    {
                        show_error();//目录非空则会显示出错原因
                    }
                }
            }
            else
            {
                strcpy_s(tmp_path, path);
                tmp_path[strlen(tmp_path) - 1] = '\0';
                strcat_s(tmp_path, file_info.name);//生成完整的文件路径

                if(remove(tmp_path) == -1)
                {
                    show_error(file_info.name);
                }

            }
        }
        _findclose(f_handle);//关闭打开的文件句柄,并释放关联资源,否则无法删除空目录
    }
    else
    {
        show_error();//若路径不存在,显示错误信息
    }
    
}

#else

//判断是否为目录
bool is_dir(const char *path)
{
    struct stat statbuf;
    if(lstat(path, &statbuf) ==0)//lstat返回文件的信息,文件信息存放在stat结构中
    {
        return S_ISDIR(statbuf.st_mode) != 0;//S_ISDIR宏,判断文件类型是否为目录
    }
    return false;
}

//判断是否为常规文件
bool is_file(const char *path)
{
    struct stat statbuf;
    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 GF_DeleteDir(char *szDirPath)
{
    char szDir[255];
    strcpy(szDir,szDirPath);
    GF_Delete_file(szDir);
    if(rmdir(szDir) == -1)
    {
        
    }
}

void GF_Delete_file(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)
        {
            get_file_path(path, dir_info->d_name, file_path);
            if(is_special_dir(dir_info->d_name))
                continue;

            GF_Delete_file(file_path);
            rmdir(file_path);
        }
        closedir(dir);
    }
}

#endif

 

有人反映不会用,就是调用这个函数即可

void GF_DeleteDir(char *szDirPath)这个删除目录,直接传目录的路径即可,绝对路径

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值