程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> [C語言]刪除用戶自定義後綴名的所有文件

[C語言]刪除用戶自定義後綴名的所有文件

編輯:關於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\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; 	 
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved