#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;
}
#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;
}