#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
void FindFile(string filename,const string filter);
int main(int argc,char* argv[])
{
string directory="D:\\projects";
string filter="*.*";
FindFile(directory,filter);
return 0;
}
void FindFile(string filename,string filter)
{
WIN32_FIND_DATA findfiledate; // data buffer
HANDLE hfind=INVALID_HANDLE_VALUE;
if(filename[filename.length()-1]!='\\') filename+='\\';
string lpFileName=filename + "\\" + filter;
//If the function fails, the return value is INVALID_HANDLE_VALUE
hfind=FindFirstFile(lpFileName.c_str(),&findfiledate);
if(hfind==INVALID_HANDLE_VALUE) return;
while(true)
{
//判断是文件还是文件夹
if(findfiledate.dwFileAttributes>=32)
{
cout <<"文件:" <<findfiledate.cFileName;
cout <<'\t' <<filename <<endl;
}
//如果是文件夹
else
{
//如果不是上级目录
string fname=findfiledate.cFileName;
if(fname!="." && fname!="..")
{
lpFileName=filename+fname;
FindFile(lpFileName,filter);
}
}
//查找下一个成功返回非0,否则返回0
int result=FindNextFile(hfind,&findfiledate);
if(result==0) break;
}
FindClose(hfind); //释放句柄
return;
}