#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<string.h>
#include<time.h>
void DiretorySearch(const char *dirPath){
WIN32_FIND_DATAA lpFindFileData;
char dirPathTemp[MAX_PATH];
char dirCodeTemp[MAX_PATH];
char FileName[MAX_PATH];
char Symbol[30] = "*";
char FileSuffixes[10];
strcpy_s(dirPathTemp, strlen(dirPath) + 1, dirPath);
strcpy_s(dirCodeTemp, strlen(dirPath) + 1, dirPath);
const char *pChar = strrchr(dirPath,'\\');
if(pChar!=NULL&&strlen(pChar)==1){
strcat_s(dirCodeTemp,"*");
}else{
strcat_s(dirCodeTemp,"\\*");
strcat_s(dirPathTemp,"\\");
}
HANDLE handle = FindFirstFileA(dirCodeTemp, &lpFindFileData);
if(handle == INVALID_HANDLE_VALUE){
printf("%s 检索失败!",dirPathTemp);
return;
}
printf("%s\n",dirPathTemp);
printf("输入要检索的文件后缀:\n");
scanf("%s",FileSuffixes);
strcat_s(Symbol,FileSuffixes);
printf("%s\n",Symbol);
printf("输入保存的文件名:\n");
scanf("%s",FileName);
strcat_s(FileName,".txt");
printf("%s\n",FileName);
char command[500] = {0};
strcat_s(command,"dir "); // dir
strcat_s(command,dirPathTemp); // 路径
strcat_s(command,Symbol); // *jpg
strcat_s(command," /b/s > "); // /b/s
strcat_s(dirPathTemp,FileName);
strcat_s(command,dirPathTemp);
printf("命令为:%s\n",command);
//计时测试
long start,end;
//计时测试开始
start = clock();
system(command);
//计时测试结束
end = clock();
//输出结果
printf("本次检索所用的时间为:%ld(单位ms)\n",end-start);
}
void main(){
char pathName[MAX_PATH];
printf("请输入目录:");
scanf("%s",pathName);
DiretorySearch(pathName);
system("pause");
}
简单的使用DOS命令实现的文件检索(列出指定后缀的所有文件夹)