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


    环境:win7

    IDE:DEV-C++

    编译器:GCC

    编译结果:Success

    运行结果:Success

    使用说明:

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

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

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

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

    源码:

    #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
    ", 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
    ", pFilePath,GetLastError());  
       	   return ;  
    	}
    	else{
    		if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY ){  
            printf("FileName:%s
    ", 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 ("
    FileName:%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
    ", 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!
    ";
    	Show(string1);
     	printf("Anleb : ");
     	sleep(1000);
     	char string2[] = "Go,gay!
    ";
    	Show(string2);
    	printf("Please Enter the Path:");
    	char path[128];
    	gets(path);
    	while(strlen(path) == 0)
    	{
    	 				printf("Warning:The Path value is Null!
    ");
    	 				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!
    ");
    	 				printf("Please Enter the ExtName:");
    	 				gets(extName);
    					}
    	if(strcmp(extName,"exit") ==0)
    		return 0;
    	findAllFile(path,extName);
    	printf("
    Delete Count: %d
    ",count);
     	system("pause");
     	return 0; 	 
    }
    
  • 相关阅读:
    数据导入和导出
    用户登陆案例
    SQLHelper
    把连接数据库的字符串放在配置文件中
    访问数据库
    SQL语句
    Django Tornado Flask
    Python 的协程
    面试 Better Call Soul
    mklink 解决VScode 扩展...Google迁移到 windows D盘
  • 原文地址:https://www.cnblogs.com/IAmBetter/p/3173942.html
Copyright © 2020-2023  润新知