c语言 删除指定文件

c语言 删除指定文件,第1张

C语言删除指定文件或目录,参考代码如下:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <io.h>

#include <direct.h>

#include <errno.h>

//判断是否是".."目录和"."目录

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:

            printf("Given path is not a directory, the directory is not empty, or the directory is either the current working directory or the root directory.\n")

            break

        case ENOENT:

            printf("Path is invalid.\n")

            break

        case EACCES:    

            printf("%s had been opend by some application, can't delete.\n", file_name)

            break

    }

}

inline void get_file_path(const char *path, const char *file_name, char *file_path)

{

    strcpy_s(file_path, sizeof(char) * _MAX_PATH, path)

    file_path[strlen(file_path) - 1] = '\0'

    strcat_s(file_path, sizeof(char) * _MAX_PATH, file_name)

    strcat_s(file_path, sizeof(char) * _MAX_PATH, "\\*")

}

//递归搜索目录中文件并删除

inline void 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)

                delete_file(tmp_path)//开始递归删除目录中的内容

                tmp_path[strlen(tmp_path) - 2] = '\0'

                if(file_info.attrib == 20)

                    printf("This is system file, can't delete!\n")

                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()//若路径不存在,显示错误信息

    }

}

int main(int argc, char **argv)

{

    delete_file("C:\\Documents and Settings\\Administrator\\Local Settings\\Temporary Internet Files\\*")

    system("pause")

    return 0

}

//调用system函数并传递字符串参数rd

/s

/q

path(path为目录的路径)就行了

//下面有一个例子

#include<stdio.h>

#include<string.h>

int

main()

{

char

cmd[256]="rd

/s

/q

"

printf("请输入要删除的目录的路径:")

//将目录的路径连接到cmd的后面

gets(cmd+strlen(cmd))

if(0==system(cmd))

printf("目录已删除,请注意查看!\n")

return

0

}

用 system 调 DOS 命令 ERASE 或 DEL

加选项 /F 强迫删除

加选项 /Q 不要问是否确定要删除

路径单斜杠用双斜杠。

例如:

system("DEL /F /Q *.jpg")-- 删除当前文件夹里所有的jpg图像文件

system("ERASE C:\\TEMP\\abc.txt")

删除文件 C:\TEMP\abc.txt

也可以:

char cmd[]="ERASE C:\\TEMP\\abc.txt"

system(cmd)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11598908.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存