一个简单的递归遍历文件夹查找文件的代码,执行后却无任何反应?请帮助!!#include <windows.h>
#include <iostream.h>void function(LPCTSTR lpszPath);
BOOL IsRoot(LPCTSTR lpszPath);void main(LPCTSTR lpszPathmain)
{
function(lpszPathmain);
}
void function(LPCTSTR lpszPath)
{
TCHAR szFind[MAX_PATH];
lstrcpy(szFind, lpszPath);
if (!IsRoot(szFind)) // IsRoot是我自己编写的函数,若参数是根目录,则返回true
lstrcat(szFind, "\\"); lstrcat(szFind, "*.txt"); // 找所有文件
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(szFind, &wfd);
if (hFind == INVALID_HANDLE_VALUE) // 如果没有找到或查找失败
return; do
{
if (wfd.cFileName[0] == '.')
continue; // 过滤这两个目录
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
TCHAR szFile[MAX_PATH]; if (IsRoot(lpszPath))
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
else
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName); function(szFile); // 如果找到的是目录,则进入此目录进行递归
}
else
{
// 对文件进行操作
cout<<wfd.cFileName;
}
} while (FindNextFile(hFind, &wfd)); FindClose(hFind); // 关闭查找句柄
}BOOL IsRoot(LPCTSTR lpszPath)
{
TCHAR szRoot[4];
wsprintf(szRoot, "%c:\\", lpszPath[0]);
return (lstrcmp(szRoot, lpszPath) == 0);
}
#include <iostream.h>void function(LPCTSTR lpszPath);
BOOL IsRoot(LPCTSTR lpszPath);void main(LPCTSTR lpszPathmain)
{
function(lpszPathmain);
}
void function(LPCTSTR lpszPath)
{
TCHAR szFind[MAX_PATH];
lstrcpy(szFind, lpszPath);
if (!IsRoot(szFind)) // IsRoot是我自己编写的函数,若参数是根目录,则返回true
lstrcat(szFind, "\\"); lstrcat(szFind, "*.txt"); // 找所有文件
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(szFind, &wfd);
if (hFind == INVALID_HANDLE_VALUE) // 如果没有找到或查找失败
return; do
{
if (wfd.cFileName[0] == '.')
continue; // 过滤这两个目录
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
TCHAR szFile[MAX_PATH]; if (IsRoot(lpszPath))
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
else
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName); function(szFile); // 如果找到的是目录,则进入此目录进行递归
}
else
{
// 对文件进行操作
cout<<wfd.cFileName;
}
} while (FindNextFile(hFind, &wfd)); FindClose(hFind); // 关闭查找句柄
}BOOL IsRoot(LPCTSTR lpszPath)
{
TCHAR szRoot[4];
wsprintf(szRoot, "%c:\\", lpszPath[0]);
return (lstrcmp(szRoot, lpszPath) == 0);
}
void CBrowseDlg::OnBtnFind()
{
UpdateData(TRUE);
g_lCount = 0;
CString csDir = m_csDir;
csDir.TrimLeft(' ');
csDir.TrimRight(' ');
m_List.ResetContent(); //把查找到的文件名添加到一个LisBox( m_List )
Browse(csDir);
csDir.Format("%d",g_lCount);
}
void CBrowseDlg::Browse(CString csDir)
{
BOOL bFile =TRUE ;
CFileFind ff;
if (csDir.Right(1)!='\\') {
csDir+='\\';
}
csDir += "*.*";
bFile = ff.FindFile(csDir);
while (bFile) {
bFile = ff.FindNextFile();
if (bFile) {
if (ff.IsDirectory()&&!ff.IsDots()) {
Browse(ff.GetFilePath());
}else if(!ff.IsDirectory()&&!ff.IsDots()){
m_List.AddString( ff.GetFilePath());
}
}
ff.Close();
}