在論壇經常碰到初學者不知道如何遍曆檔案,其實很簡單,只要使用下面的代碼就可以了。
void ChkRecurse(LPCTSTR pstr /* = NULL */){ CFileFind finder; // build a string with wildcards CString strWildcard(pstr); strWildcard += _T("\\*.*"); // start working for files BOOL bWorking = FALSE; try { bWorking = finder.FindFile(strWildcard); } catch (CException* e) { char szCause[255] = {0}; e->GetErrorMessage(szCause, 255); Log->LogOutEx(szCause); } while (bWorking) { try { bWorking = finder.FindNextFile(); // skip . and .. files; otherwise, we'd // recur infinitely! if (finder.IsDots()) // '.' and '..' continue; if (finder.IsSystem())// 系統檔案 continue; else if (finder.IsDirectory()) //目錄 { CString str = finder.GetFilePath(); CString str1; GetDirName(str,str1); if ( str1.GetLength() == 12 && _is_num(str1) ) { ChkRecurse(str);//回調 } else { Sleep(20); continue; } } else {//檢查檔案 //Do your work... } } catch (CException* e) { char szCause[255] = {0}; e->GetErrorMessage(szCause, 255); Log->LogOutEx(szCause); } } finder.Close(); }
有不明白的地方歡迎評論、留言。