1 #ifndef __FILE_ENUMERATOR_H_EEF8FBA5_B0F1_4682_9614_2027C16002F4
2 #define __FILE_ENUMERATOR_H_EEF8FBA5_B0F1_4682_9614_2027C16002F4
4 #include "PathHelper.h"
6 typedef bool (*PFN_EnumeratorCallback)(LPCTSTR lpszItemName, LPCTSTR lpszItemDir, LPWIN32_FIND_DATA lpszItemFindData, LPVOID lpUserData);
13 CString m_strSearchDir;
15 CString m_strFileToSearch;
17 bool m_bRecurseSubDirectories;
21 bool m_bReportMatchingDirsAlso;
23 PFN_EnumeratorCallback m_pfnEnumCallback;
25 inline static bool EnumCallback(LPCTSTR, LPCTSTR, LPWIN32_FIND_DATA, LPVOID)
31 CFileEnumerator(PFN_EnumeratorCallback pfnEnumCallback = CFileEnumerator::EnumCallback,
32 LPCTSTR lpszSearchDir = _T(
"."),
33 LPCTSTR lpszFileToSearch = _T(
"*.*"),
34 bool bRecurseSubDirectories =
true,
35 bool bReportMatchingDirsAlso =
true,
36 bool bIgnoreDots =
true
39 m_pfnEnumCallback = pfnEnumCallback;
40 m_strSearchDir = lpszSearchDir;
41 m_strFileToSearch = lpszFileToSearch;
42 m_bRecurseSubDirectories = bRecurseSubDirectories;
43 m_bReportMatchingDirsAlso = bReportMatchingDirsAlso;
44 m_bIgnoreDots = bIgnoreDots;
51 PFN_EnumeratorCallback& FileEnumeratorCallback()
53 return m_pfnEnumCallback;
56 bool& IgnoreDots() {
return m_bIgnoreDots; }
58 bool& RecurseSubDirectories() {
return m_bRecurseSubDirectories; }
60 CString& FileToSearch() {
return m_strFileToSearch; }
62 LPCTSTR FileToSearch()
const {
return m_strFileToSearch; }
64 CString& SearchDir() {
return m_strSearchDir; }
66 LPCTSTR SearchDir()
const {
return m_strSearchDir; }
68 bool& ReportMatchingDirsAlso() {
return m_bReportMatchingDirsAlso; }
70 HANDLE FindFirstSubDir(LPCTSTR lpszDir, LPWIN32_FIND_DATA pFindData)
const
72 HANDLE hFind = FindFirstFile(CString(lpszDir) + _T(
"*.*"), pFindData);
73 if(hFind == INVALID_HANDLE_VALUE)
return hFind;
77 if(PathIsDots(pFindData->cFileName))
79 if(m_bIgnoreDots ==
false)
84 if(!(pFindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
continue;
88 }
while(FindNextFile(hFind, pFindData));
90 return INVALID_HANDLE_VALUE;
95 while(FindNextFile(hFind, pFindData))
97 if(PathIsDots(pFindData->cFileName))
99 if(m_bIgnoreDots ==
false)
104 if(!(pFindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
continue;
116 bool ListAllDirectories(LPVOID lpUserData = NULL)
const
118 WIN32_FIND_DATA ffData;
120 CString strSearchDir((LPCTSTR)SearchDir());
121 PathAddBackSlash(strSearchDir);
123 HANDLE hFind = FindFirstSubDir(strSearchDir, &ffData);
124 if(hFind == INVALID_HANDLE_VALUE)
return true;
126 bool bContinue =
true;
130 if(
false == (*m_pfnEnumCallback)(ffData.cFileName, strSearchDir, &ffData, lpUserData))
133 if(m_bRecurseSubDirectories)
135 if(PathIsDots(ffData.cFileName))
continue;
138 strSearchDir + ffData.cFileName,
140 m_bRecurseSubDirectories,
141 m_bReportMatchingDirsAlso,
143 bContinue = feDir.ListAllDirectories(lpUserData);
158 bool EnumerateFiles(LPVOID lpUserData = NULL)
const
160 bool bContinue =
true;
162 CString strSearchDir((LPCTSTR)SearchDir());
164 PathAddBackSlash(strSearchDir);
166 WIN32_FIND_DATA ffData;
168 HANDLE hFind = FindFirstFile(strSearchDir + (LPCTSTR)FileToSearch(), &ffData);
169 if(hFind == INVALID_HANDLE_VALUE)
171 if(m_bRecurseSubDirectories)
176 m_bRecurseSubDirectories,
177 m_bReportMatchingDirsAlso,
179 hFind = feDir.FindFirstSubDir(strSearchDir, &ffData);
180 if(hFind == INVALID_HANDLE_VALUE)
return true;
184 strSearchDir + ffData.cFileName,
186 m_bRecurseSubDirectories,
187 m_bReportMatchingDirsAlso,
188 m_bIgnoreDots).EnumerateFiles(lpUserData);
189 }
while(bContinue && feDir.FindNextSubDir(hFind, &ffData));
201 if(PathIsDots(ffData.cFileName))
203 if(m_bIgnoreDots ==
false)
204 bContinue = (*m_pfnEnumCallback)(ffData.cFileName, strSearchDir, &ffData, lpUserData);
208 if((ffData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
210 if(m_bReportMatchingDirsAlso)
211 bContinue = (*m_pfnEnumCallback)(ffData.cFileName, strSearchDir, &ffData, lpUserData);
215 bContinue = (*m_pfnEnumCallback)(ffData.cFileName, strSearchDir, &ffData, lpUserData);
218 }
while(bContinue && FindNextFile(hFind, &ffData));
222 if(bContinue && m_bRecurseSubDirectories)
227 m_bRecurseSubDirectories,
228 m_bReportMatchingDirsAlso,
230 hFind = feDir.FindFirstSubDir(strSearchDir, &ffData);
231 if(hFind == INVALID_HANDLE_VALUE)
return true;
235 strSearchDir + ffData.cFileName,
237 m_bRecurseSubDirectories,
238 m_bReportMatchingDirsAlso,
239 m_bIgnoreDots).EnumerateFiles(lpUserData);
240 }
while(bContinue && feDir.FindNextSubDir(hFind, &ffData));
BOOL FindNextSubDir(HANDLE hFind, LPWIN32_FIND_DATA pFindData) const