环境: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. 【mysql】一次有意思的数据库查询分析。

    本文是在做一家汽车配件的电商网站时,大体情景是一个List.php页面,该页面分页列出部分配件并统计总数量用于分页. 当然该页面中也可以指定一下查询条件,如适配的车辆品牌.车系.排量.年份等,一件商品 ...

  2. Linux下Vi/Vim的使用方法

    本文介绍了vi (vim)的基本使用方法,但对于普通用户来说基本上够了!i/vim的区别简单点来说,它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特 ...

  3. Visio2010如何安装

    双击setup.   点击我接受此协议的条款,然后点击继续.   这里选择自定义,很重要哦,不要选择立即安装,不然,一会装完后,你会找不到快捷方式的.   文件位置这里选择好存放路径,一会我们要去这里 ...

  4. 剑指 offer set 27 赋值运算符函数

    要求为类 CMyString 定义赋值运算符函数. 类的定义如下 class CMyString { public: CMyString(char* pData = NULL; ) CMyString ...

  5. 简单是Jedis实例(相对连接池而言)

    在引入相关jar包后,只要new一个Jedis对象,就能做redis相关操作了.以下是一个简单的jedis实例: package com.pptv.redis; import java.util.Ar ...

  6. 如何改变placeholder的样式

    input::-webkit-input-placeholder { color: #B2B2B2; } input:-moz-placeholder { color: #B2B2B2; } inpu ...

  7. jsp导出到Excel

    jsp模板文件 <%@ page isELIgnored="false" contentType="application/x-msdownload; charse ...

  8. 教你在Ubuntu上体验Mac风格

    导读 老实说,我是个狂热的 Ubuntu 迷,我喜欢 Ubuntu 默认的 Unity 主题样式外观.此外,还有很多关于 Ubuntu 14.04 的漂亮图标主题样式 可用来美化默认的外观.但正如我上 ...

  9. 工作表(Worksheet)基本操作应用示例

    在编写代码时,经常要引用工作表的名字.知道工作表在工作簿中的位置.增加工作表.删除工作表.复制工作表.移动工作表.重命名工作表,等等.下面介绍与此有关及相关的一些属性和方法示例. [示例04-01]增 ...

  10. 【BZOJ3211】花神游历各国 并查集+树状数组

    [BZOJ3211]花神游历各国 Description Input Output 每次x=1时,每行一个整数,表示这次旅行的开心度 Sample Input 41 100 5 551 1 22 1 ...