#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
void FindFile(string strPath);
int main()
{
string disk;
string dir;
cout<<"请输入文件夹所在盘:(如在D盘输入D或d)"<<endl;
cin>>disk;
cout<<"请输入文件夹名:"<<endl;
cin>>dir;
string strFilePath=disk+":\\"+dir+"\\";
string strFindPath=strFilePath+"*.*";
WIN32_FIND_DATA findData;
HANDLE hFindFile=FindFirstFile(strFindPath.c_str(),&findData);
if(INVALID_HANDLE_VALUE==hFindFile)
printf("Error:%d\n",GetLastError());
while(FindNextFile(hFindFile,&findData))
{
if(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if(findData.cFileName[0]=='.')
{
continue;
}
cout<<"在此文件夹 "<<" 找到文件夹: "<<findData.cFileName<<endl;
string strNeedFindPath=strFilePath+findData.cFileName;
FindFile(strNeedFindPath);
}
else
{
cout<<"在此文件夹 "<<" 找到文件: "<<findData.cFileName<<endl;
}
}
FindClose(hFindFile);
system("pause");
return 0;
}
void FindFile(string strPath)
{
string strFindPath=strPath+"\\*.*";
WIN32_FIND_DATA findData;
HANDLE hFindFile=FindFirstFile(strFindPath.c_str(),&findData);
if(INVALID_HANDLE_VALUE==hFindFile)
printf("Error:%d",GetLastError());
while(FindNextFile(hFindFile,&findData))
{
if(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if(findData.cFileName[0]=='.')
{
continue;
}
cout<<"在 "<<strPath<<" 找到文件夹:"<<findData.cFileName<<endl;
string strNeedFindPath=strPath+"\\"+findData.cFileName;
FindFile(strNeedFindPath);
}
else
{
cout<<"在 "<<strPath<<" 找到文件:"<<findData.cFileName<<endl;
}
}
FindClose(hFindFile);
}