环境: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;
}

[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. java中不用BigInteger实现超大整数的乘法操作

    昨天看到一个题目:计算1234!,不能用BigInteger类 众所周知阶乘的数据会非常大,经常使用的int和long型根本不够用.一般想到的仅仅有BigInteger类,可是题目中明白说了不能用,所 ...

  2. (转)java泛型

    转自:http://blog.csdn.net/lonelyroamer/article/details/7868820  java泛型(二).泛型的内部原理:类型擦除以及类型擦除带来的问题 参考:j ...

  3. hdu 4291(矩阵+暴力求循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4291 思路:首先保留求出循环节,然后就是矩阵求幂了. #include<iostream> ...

  4. UE4打包程序没有声音-需要安装UE4PrereqSetup_x64.exe

    一个UE4工程打包之后,放到一台新机器,最好安装一下UE4自带的Prerequisites,否则可能会出现没有声音的问题 此安装程序位于WindowsNoEditor\Engine\Extras\Re ...

  5. 使用 awk 过滤文本或文件中的字符串

    当我们在 Unix/Linux 下使用特定的命令从字符串或文件中读取或编辑文本时,我们经常需要过滤输出以得到感兴趣的部分.这时正则表达式就派上用场了. 什么是正则表达式? 正则表达式可以定义为代表若干 ...

  6. 【BZOJ1441】Min 拓展裴蜀定理

    [BZOJ1441]Min Description 给出n个数(A1...An)现求一组整数序列(X1...Xn)使得S=A1*X1+...An*Xn>0,且S的值最小 Input 第一行给出数 ...

  7. Thymeleaf模板如何获取springMVC返回的model值

    Thymeleaf模板如何获取springMVC返回的model值 后台的实现: @RequestMapping("/hello") public String hello(Mod ...

  8. js apply()、call() 使用参考

    引入,求一个数组的最大值,有这么一种快捷方法:Math.max.apply(null,arr); 但是最初看 JavaScript高级程序设计 的时候,没看懂,原文(斜体表示)如下: 每个函数都包含两 ...

  9. 160304-01、mysql数据库插入速度和读取速度的调整记录

    需求:由于项目变态,需要在一个比较短时间段急剧增加数据库记录(两三天内,由于0增加至5亿).在整个过程调优过程非常艰辛 思路: (1)提高数据库插入性能中心思想:尽量将数据一次性写入到Data Fil ...

  10. JSONP使用

    一.什么是JSONP jsonp是一种规则,它是利用创建html的script快的方式,将远端url放到src属性中,并以函数的形式执行远程返回值中的函数. jsonp的出现是为了解决浏览器同源策略的 ...