参考了论坛里高手们的代码,自己做了小修改~~~
#include <stdio.h>
#include <afx.h>
#include <afxwin.h>
#include <iostream>
#include <string>
using namespace std;
void RecursiveFindFile(CString strPath)
{
CFileFind ff;
BOOL bFind = ff.FindFile(strPath + "\\*.*");
while(bFind)
{
bFind = ff.FindNextFile();
if (ff.IsDots() || ff.IsSystem() || ff.IsHidden())
continue;
if (ff.IsDirectory())
{
RecursiveFindFile(ff.GetFilePath());
}
else
{
CString strFileName = ff.GetFilePath();
AfxMessageBox(strFileName);
}
}
}
int main()
{
string pathin;
CString spath;
cout<<"Please enter the path (Like C:\\):"<<endl;
while(cin>>pathin)
{
spath=pathin.c_str();
break;
}
RecursiveFindFile(spath);
system("pause");
return 0;
}
当输入需要遍历的路径后,开始以消息框的形式给出文件名称,大家可以拿去改一改,用来保存文件名及文件信息。
注意,后来测试了一下,当文件路径含有空格时,因为是string类型保存路径,所以会出现错误,可以修改一下输入的方式,采用getline()函数。