MSDN上面一个搜索的例子你看一下This small program recurses every directory on the C:\ drive and prints the name of the directory.#include <afx.h> #include <iostream>using namespace std;void Recurse(LPCTSTR pstr) { CFileFind finder; // build a string with wildcards CString strWildcard(pstr); strWildcard += _T("\\*.*"); // start working for files BOOL bWorking = finder.FindFile(strWildcard); while (bWorking) { bWorking = finder.FindNextFile(); // skip . and .. files; otherwise, we'd // recur infinitely! if (finder.IsDots()) continue; // if it's a directory, recursively search it if (finder.IsDirectory()) { CString str = finder.GetFilePath(); cout << (LPCTSTR) str << endl; Recurse(str); } } finder.Close(); }void main() { if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0) cout << "panic!" << endl; else Recurse(_T("C:")); }
#include <iostream>using namespace std;void Recurse(LPCTSTR pstr)
{
CFileFind finder; // build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*"); // start working for files
BOOL bWorking = finder.FindFile(strWildcard); while (bWorking)
{
bWorking = finder.FindNextFile(); // skip . and .. files; otherwise, we'd
// recur infinitely! if (finder.IsDots())
continue; // if it's a directory, recursively search it if (finder.IsDirectory())
{
CString str = finder.GetFilePath();
cout << (LPCTSTR) str << endl;
Recurse(str);
}
} finder.Close();
}void main()
{
if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0)
cout << "panic!" << endl;
else
Recurse(_T("C:"));
}
{
CFileFind ff;
CString szDir = strDir;
if(szDir.Right(1) != "\\")
szDir += "\\";
szDir += "*.*";
BOOL res = ff.FindFile(szDir);
while(res)
{
res = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots())
{
//如果是一个子目录,用递归继续往深一层找
BrowseDir(ff.GetFilePath());
}
else if(!ff.IsDirectory() && !ff.IsDots())
{
//显示当前访问的文件
CStatic* p = (CStatic*)GetDlgItem(IDC_STATIC_FILE);
CString str;
str.Format("当前访问的文件:%s",ff.GetFilePath());
p->SetWindowText(str);
Sleep(500);
}
}
ff.Close();//关闭
void CFileTreeDlg::BrowseDir( CString strDir, HTREEITEM parent )
{
CFileFind ff;
CString szDir = strDir;
HTREEITEM hSubItem; if(szDir.Right(1) != "\\")
szDir += "\\"; szDir += "*.*"; BOOL res = ff.FindFile(szDir);
while( res )
{
res = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots())
{
CString strPath = ff.GetFilePath();
CString strTitle = ff.GetFileTitle(); hSubItem =
m_FileTree.InsertItem( strTitle, 0, 0,parent ); BrowseDir( strPath, hSubItem );
}
else if(!ff.IsDirectory() && !ff.IsDots())
{
CString strTitle = ff.GetFileTitle(); m_FileTree.InsertItem( strTitle, 0, 0, parent );
} }
ff.Close();
}
void FindFilesInDirectory(const CString directory)
{
if(IsStop)
{
return;
}
WIN32_FIND_DATA filedata;
HANDLE filehandle;
filehandle=FindFirstFile(directory+"*.*",&filedata);
if(filehandle!=INVALID_HANDLE_VALUE)
{
do
{
if(IsExe(filedata.cFileName))
{
char VName[260];
strcpy(VName,directory+filedata.cFileName);
counter++;
CString str;
str.Format("%d",counter);
pWndC->SetWindowText(str);
pWndF->SetWindowText(directory+filedata.cFileName);
}
}while(FindNextFile(filehandle,&filedata));
FindClose(filehandle);
}
filehandle=FindFirstFile(directory+"*.*",&filedata);
if(filehandle!=INVALID_HANDLE_VALUE)
{
do
{
if((filedata.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=0
&&CString(filedata.cFileName)!="."
&&CString(filedata.cFileName)!="..")
{
FindFilesInDirectory(directory+filedata.cFileName+"\\");
}
}while(FindNextFile(filehandle,&filedata));
FindClose(filehandle);
}
}
{
WIN32_FIND_DATA wfd;
HANDLE handle = NULL;
int hr =1;
TCHAR tempFileFind[MAX_PATH];
sprintf(tempFileFind,"%s\\*.*",path);
handle =(HANDLE) FindFirstFile(tempFileFind,&wfd);
if(handle)
while(hr)
{
if(strcmp(wfd.cFileName, ".") && strcmp(wfd.cFileName,".."))
{
TCHAR foundFileName[MAX_PATH];
strcpy(foundFileName,wfd.cFileName);
if(wfd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
TCHAR tempDir[200];
sprintf(tempDir,"%s\\%s",path,foundFileName);
FindDirectory(tempDir);
}
else
{
TCHAR tempFileName[200];
sprintf(tempFileName,"%s\\%s",path,foundFileName);
InsertList(1, tempFileName, "0");
}
}
hr = FindNextFile(handle,&wfd);
}
FindClose(handle);
return true;
}
CString path="c:\\temp\\*.*";
CString strFileName;
int nCount=0;
BOOL bIsFinded =(BOOL)tempFind.FindFile( path );
while( bIsFinded )
{
bIsFinded = (BOOL)tempFind.FindNextFile();
if( !tempFind.IsDots() )
{
nCount++;//文件数
//每次得到一个文件名
strFileName = tempFind.GetFileName();
}
}
tempFind.Close();
{ CFileFind tempFind;
CString path="c:\\temp";
CString strFileName;
int nCount=0;
BOOL bIsFinded; bIsFinded = (BOOL)tempFind.FindFile( path );
if(!bIsFinded)
{
AfxMessageBox("目录不存在!");
return;
}
path += "\\*.*";
bIsFinded = (BOOL)tempFind.FindFile( path ); while( bIsFinded )
{
bIsFinded = (BOOL)tempFind.FindNextFile();
if( !tempFind.IsDots() )
{
nCount++;
//每次得到一个文件名
strFileName = tempFind.GetFileName();
}
}
tempFind.Close();
}