环境: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. 透過 bc 計算 pi

    echo "scale=${num}; 4*a(1)" | bc -lq例如: echo "scale=5000; 4*a(1)" | bc -lq 4*a(1 ...

  2. Mac终端Screen命令使用指南

    (1)创建会话 使用命令“screen -S RunWork”来创建一个screen会话,命令执行之后,就会得到一个新的shell窗口,为了便于标示可以用快捷键Ctrl-a A(就是按下Ctrl+a键 ...

  3. 基于java 的websocket的聊天功能,一开始初始化websocket,执行打开连接之后就直接关闭连接了。

    1 错误描述: java 后台没有报错,但是就是连接不上,一连上又自动关闭. 2 错误根源: 缺少jar包. 对比了报错的tomcat 的jar包和不报错的jar包 发现是tomcat下缺少上图绿色框 ...

  4. phpredis 中文手册和redis 教程

    phpredis 中文手册  :   http://www.cnblogs.com/zcy_soft/archive/2012/09/21/2697006.html 手册: http://www.cn ...

  5. Android - ViewPager实现Gallery效果

    RelativeLayout viewPagerContainer = (RelativeLayout) headerView.findViewById(R.id.content_pager_layo ...

  6. 云服务器 ECS Linux 保存用户登录操作命令记录

    转载自 : https://help.aliyun.com/knowledge_detail/41210.html 云服务器 ECS Linux 如果要保存用户登录操作记录,则可以通过在 /etc/p ...

  7. ajax请求加全局loading , 个别特殊请求不显示loading

    项目中,请求开始前加载loading遮罩层,请求结束关闭遮罩,一般都会加在全局中,但有个别请求不需要加全局loading 的话,这时候就需要对这些请求进行配置 全局加loading: ; functi ...

  8. 160531、SQL优化-索引

    SQL优化有很多方法,今天来说一说数据库索引. 举例说明: 假设有一个图书Book表,里面有字段id,name, isbn等.如果图书数量巨大的话,我们通过isbn查询通常是比较慢的. 添加数据库索引 ...

  9. kibana5.6 源码分析以--环境搭建&技术准备

    Kibana是一个开源的分析与可视化平台,设计出来用于和Elasticsearch一起使用的.你可以用kibana搜索.查看.交互存放在Elasticsearch索引里的数据,使用各种不同的图表.表格 ...

  10. 20165330 2017-2018-2 《Java程序设计》第4周学习总结

    课本知识总结 第五章 子类与继承 子类:在类的声明中,通过使用关键字extends来定义一个类的子类. class 子类名 extends 父类名 { ... } 继承:子类继承父类的成员变量作为自己 ...