C语言_利用DOS命令实现的一个简单文件检索

#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命令实现的文件检索(列出指定后缀的所有文件夹)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值