环境:win7

IDE:DEV-C++

编译器:GCC

编译结果:Success

运行结果:Success

使用说明:

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

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

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

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

源码:

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

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

  1. -05 08:57 ARCGIS地统计学计算文件后缀名为.shp文件制作

    2011-07-05 08:57 ARCGIS地统计学计算文件后缀名为.shp文件制作 ARCAMP软件要进行地统计计算的文件后格式后缀名必须为.shp的文件,网上介绍的方法复杂难懂,那么制作.shp ...

  2. IO流-递归遍历目录下指定后缀名结尾的文件名称

    /* *自定义遍历目录下指定后缀名结尾文件的名称的方法: * * param file:指定目录 name:指定后缀名 */ 1 public static void FileName(File fi ...

  3. C#中获取指定路径下指定后缀名的所有文件的路径的list

    场景 指定一个路径和后缀名,查找这个路径下所有以此后缀名结尾的文件. 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程序猿 获取 ...

  4. linux下递归删除指定后缀名的文件

    以删除当前目录到所有子目录下的后缀名为rej的文件为例: find . -name "*.rej" |xargs rm -f

  5. linux shell中如何删除指定后缀名的文件?

    答: find . -name '*.txt' -delete 这条命令含义如下: 从当前目录开始查找以txt为后缀名的文件并删除掉

  6. [Java] 03 String获取文件后缀名,判断文件是否合法

    package test.string; import java.util.Arrays; import java.util.List; public class GetFileType { priv ...

  7. Java获取一个路径下指定后缀名的所有文件

    方法一: http://blog.csdn.net/zjx102938/article/details/8114114 import java.io.File; import java.util.Ar ...

  8. linux命令(13) 删除指定文件夹下后缀名相同的文件

    方法一: find 目录 -name "*.abc" | xargs rm命令有点危险,可以先执行前半段,看看是不是你要删除的文件, 然后再整条执行 方法二:find . -nam ...

  9. 删除linux上7天前后缀名.sql的文件

    #!/bin/bash#delete the file of 7 days agofind /data/mysqlbackup/ -mtime +7 -name "*.sql" - ...

随机推荐

  1. linux下解压 tar.bz2

    tar xvfj xxx.tar.bz2 转自: http://www.360doc.com/content/12/0907/16/8006573_234845810.shtml

  2. sublime text 2自定义代码片段

    本文引用   http://www.blogjava.net/Hafeyang/archive/2012/08/17/how_to_create_code_snippet_in_subline_tex ...

  3. jQuery制作简洁的多级联动Select下拉框

    今天我们要来分享一款很实用的jQuery插件,它是一个基于jQuery多级联动的省市地区Select下拉框,并且值得一提的是,这款联动下拉框是经过自定义美化过的,外观比浏览器自带的要漂亮许多.另外,这 ...

  4. JQuery------实现鼠标摁下抬起时div背景色改变

    作用:使用自定义一个按钮 代码: <div class = 'btn'>按钮</div> $(".btn").mousedown(function () { ...

  5. win7下maven的安装

    1.在安装maven之前,先确保已经安装JDK1.6及以上版本,并且配置好环境变量.2.上Maven官网(https://maven.apache.org/download.cgi)下载Maven的压 ...

  6. Spring中bean的作用范围

    singleton作用域: Spring的scope的默认值是singleton Spring 只会为每一个bean创建一个实例,并保持bean的引用. <bean id="bean的 ...

  7. 【BZOJ4872】[Shoi2017]分手是祝愿 数学+期望DP

    [BZOJ4872][Shoi2017]分手是祝愿 Description Zeit und Raum trennen dich und mich. 时空将你我分开.B 君在玩一个游戏,这个游戏由 n ...

  8. ps -aux | egrep 多个值

    ps -aux |egrep "(schedule.jar|positec.jar|time_server.jar|tomcat-xweb/)"

  9. 阿里云服务器Mysql修改密码

    核心架构基于阿里云服务器CentOS7.2操作系统,全面支持Java Web运行环境   Tomcat 8.5.8 JDK 8u111 MySQL5.7.16 详细信息,请在服务器执行  more r ...

  10. Codeforces Round #513-ABCD

    ABC现场做出,涨了八十几分吧.D有点思路不知道怎么实现,赛后看题解发现巨简单,想得太复杂了.蓝瘦. A----http://codeforces.com/contest/1060/problem/A ...