[C语言]删除用户自定义后缀名的所有文件

环境:win7

IDE:DEV-C++

编译器:GCC

编译结果:Success

运行结果:Success

 

使用说明:

1.输入需要查询的目录,比如e:

2.输入需要删除的后缀名:比如:txt

 

注意:本程序使用Remove删除文件,所以删除的文件不会进回收站。

 

 

程序:https://files.cnblogs.com/IAmBetter/DeleteEverything.rar

源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <direct.h> //_getcwd(), _chdir()
#include <stdlib.h> //_MAX_PATH, system()
#include <io.h>     //_finddata_t, _findfirst(), _findnext(), _findclose()
#include <string.h>
#include <windows.h>
 
//删除总数
int count = 0;
 
//获取当前路径
void GetCurrentPath(void)
{
      char buf[80];
      getcwd(buf, sizeof(buf));
      printf("current working directory : %s\n", buf);
}
 
  
//获取后缀名
char *substr(const char*str)
{
    char *ptr, c = '.';
    static char stbuf[256];
    ptr = strrchr(str, c); //最后一个出现c的位置
    if(ptr == NULL){
       return stbuf;
       }
    int pos = ptr-str;//用指针相减 求得索引
    unsigned start = pos + 1;
    unsigned end = strlen(str);
    unsigned n = end - start;
    strncpy(stbuf, str + start, n);
    stbuf[n] = 0; //字串最后加上0
    return stbuf;
}
 
//递归查询文件并且删除
void findAllFile(char *pFilePath,char *extName)
    WIN32_FIND_DATA FindFileData; 
    DWORD dwError; 
    HANDLE hFind = INVALID_HANDLE_VALUE; 
    char DirSpec[MAX_PATH+1];
    strncpy(DirSpec, pFilePath, strlen(pFilePath) + 1); 
    SetCurrentDirectory(pFilePath); 
    strncat(DirSpec, "\\*", 3);
    hFind = FindFirstFile(DirSpec, &FindFileData); 
    if (hFind == INVALID_HANDLE_VALUE){
       printf ("FileName:%s    Invalid file handle. Error is %u\n", pFilePath,GetLastError()); 
       return
    }
    else{
        if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY ){ 
        printf("FileName:%s\n", FindFileData.cFileName);
        }
        else if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY&& strcmp(FindFileData.cFileName, ".") != 0&& strcmp(FindFileData.cFileName, "..") != 0){
        char Dir[MAX_PATH + 1]; 
        strcpy(Dir, pFilePath); 
        strncat(Dir, "\\", 2); 
        strcat(Dir, FindFileData.cFileName);
        findAllFile(Dir,extName); 
        }
        while (FindNextFile(hFind, &FindFileData) != 0){
            if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY){
                 _chdir( pFilePath );
                 char *extname2 = substr(FindFileData.cFileName);
                 if(strcmp(extname2,extName) ==0){
                     printf ("\nFileName:%s ", FindFileData.cFileName);
                     int result = remove(FindFileData.cFileName);
                     if(result == 0)
                     {
                               printf("Delete Result:%d",result);
                               count++;
                               }
                      else{
                          perror("remove");
                          }
                     }
            
            else if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && strcmp(FindFileData.cFileName, ".") != 0&& strcmp(FindFileData.cFileName, "..") != 0){ 
                char Dir[MAX_PATH + 1]; 
                strcpy(Dir, pFilePath); 
                strncat(Dir, "\\", 2); 
                strcat(Dir, FindFileData.cFileName);
                findAllFile(Dir,extName); 
            }
        }
        dwError = GetLastError(); 
        FindClose(hFind); 
        if (dwError != ERROR_NO_MORE_FILES) { 
            printf ("FindNextFile error. Error is %u\n", dwError); 
            return
        
    
 
 
//开始显示部分
void Show(char str[])
{
     int i,len;
     len = strlen(str);
     for(i=0;i<len;i++)
     {
         printf("%c",str[i]);
         sleep(100);
     }
 }
  
  
int main(void)
{
    printf("Anleb : ");
    sleep(1000);
    char string1[] = "I am Anleb,nice to somthing!\n";
    Show(string1);
    printf("Anleb : ");
    sleep(1000);
    char string2[] = "Go,gay!\n";
    Show(string2);
    printf("Please Enter the Path:");
    char path[128];
    gets(path);
    while(strlen(path) == 0)
    {
                    printf("Warning:The Path value is Null!\n");
                    printf("Please Enter the Path:");
                    gets(path);
                    }
    if(strcmp(path,"exit") ==0)
        return 0;
    printf("Please Enter the ExtName:");
    char extName[10];
    gets(extName);
    while(strlen(extName) == 0)
    {
                    printf("Warning:The ExtName value is Null!\n");
                    printf("Please Enter the ExtName:");
                    gets(extName);
                    }
    if(strcmp(extName,"exit") ==0)
        return 0;
    findAllFile(path,extName);
    printf("\nDelete Count: %d\n",count);
    system("pause");
    return 0;   
}

 

posted @   Anleb  阅读(3404)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示