[C语言]删除用户自定义后缀名的所有文件
来源:互联网 发布:易建联职业生涯数据 编辑:程序博客网 时间:2024/05/21 11:08
环境: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; }
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- [C语言]删除用户自定义后缀名的所有文件
- linux 删除所有后缀名相同的文件
- 用批处理 将文件夹下的所有指定后缀名的文件,修改成自定义后缀名
- 替换文件后缀名(C语言)
- 将用户输入在控制台上的指定路径下所有的.txt文件复制到c盘下随意目录(你可以自己指定路径)并在复制后将.txt后缀名更改成.java后缀名
- java 搜索指定后缀名的所有文件
- Linux删除指定后缀名的文件
- python 删除后缀名文件
- C语言 递归地删除一个指定目录下所有指定类型的文件
- C实现获取文件的后缀名
- C实现获取文件的后缀名
- 批量修改指定文件夹下所有后缀名的文件为另外后缀名的java代码
- java批量修改指定文件夹下所有后缀名的文件为另外后缀名的代码
- java实现批量修改指定文件夹下所有后缀名的文件为另外后缀名的代码
- 获取目录中所有含有特定后缀名的文件
- python列出指定文件夹下所有给定后缀名的文件
- Qt遍历目录的所有文件,并过滤后缀名
- C语言递归删除某个目录下所有的内容
- vlc-android编译问题之live555
- 面试算法题:忘我之乘积分析
- Spring下载地址 http://www.springsource.org/download/community
- eclipse找开android工程有用的方法
- Sitemap(站点地图)你会生成吗?
- [C语言]删除用户自定义后缀名的所有文件
- linux下遍历文件夹中的文件
- Linux下查找文件的命令(以Ubuntu12.04为例)
- ASP.NET中生成rss.xml你会吗?
- MySQL数据库无法远程连接的解决办法
- Python 与目录共事
- android 命令
- XP系统怎样删除新增加的用户账号
- 【翻译】WPF4.5新特性(MSDN的翻译读不太懂)