C++实现递归对指定磁盘目录进行遍历

#include<io.h>
#include<string.h>
#include<iostream>
using namespace std;
const int Maxlen=1024;//定义最大目录长度
unsigned long FileCount=0;//记录文件数量




//递归指定目录
void FilePath(const char* Data,const char* FileName,bool * True)
{
_finddata_t Fdata;//文件结构查找对象
long Done;
char SavePath[_MAX_PATH]={0};//保存目录
strcat(SavePath,Data);//连接字符串
strcat(SavePath,"\\*.*");
Done=_findfirst(SavePath,&Fdata);
if (Done!=-1)
{
int Num=0;
while (Num!=-1)
{
if (Fdata.attrib!=_A_SUBDIR)
{
if (strcmp(Fdata.name,"...")!=0 && strcmp(Fdata.name,"..")!=0 && strcmp(Fdata.name,".")!=0)//过滤
{
char Dir[_MAX_PATH]={0};
strcat(Dir,Data);
strcat(Dir,"\\");
strcat(Dir,Fdata.name);
cout<<Dir<<endl;
FileCount++;
if (strcmp(Fdata.name,FileName)==0)
{
break;
}
}
}
Num=_findnext(Done,&Fdata);
if (Fdata.attrib==_A_SUBDIR && Num!=-1)
{
if (strcmp(Fdata.name,"...")!=0 && strcmp(Fdata.name,".")!=0 && strcmp(Fdata.name,".")!=0)
{
char Dir[_MAX_PATH]={0};
strcat(Dir,Data);
strcat(Dir,"\\");
strcat(Dir,Fdata.name);
FilePath(Dir,FileName,True);//递归
if (*True)
{
break;
}
}
}
}
}
}


int main(int Numbers,char *Str[])
{
while (true)
{
int FileCounts=0;
char FileDirs[128]={0};//保存目录
char FileNames[128]={0};
bool IsFind=false;
cout<<"请输入目录:"<<endl;
cin>>FileDirs;
cout<<"请输入文件名:"<<endl;
cin>>FileNames;
if (strcmp(FileDirs,"e")==0)
{
break;
}
FilePath(FileDirs,FileNames,&IsFind);
cout<<"文件数量:"<<FileCounts<<endl;
}
system("pause");
return  0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值