bool CollectPathsFromDirectory(const TCHAR *pattern, StrVec& paths, bool dirsInsteadOfFiles) { ScopedMem<TCHAR> dirPath(path::GetDir(pattern)); WIN32_FIND_DATA fdata; HANDLE hfind = FindFirstFile(pattern, &fdata); if (INVALID_HANDLE_VALUE == hfind) return false; do { bool append = !dirsInsteadOfFiles; if ((fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) append = dirsInsteadOfFiles && !IsSpecialDir(fdata.cFileName); if (append) paths.Append(path::Join(dirPath, fdata.cFileName)); } while (FindNextFile(hfind, &fdata)); FindClose(hfind); return paths.Count() > 0; }
// Returns a full path of the next file // Returns nullptr if finished iteration. // Returned value is valid only until we call Next() again. const WCHAR *DirIter::Next() { // when we enter here, currFindData has info for an entry // we haven't processed yet (filled by StartDirIter() or // ourselves at the end) unless foundNext is false currPath.Set(nullptr); while (foundNext && !currPath) { WCHAR *f = currFindData.cFileName; if ((currFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { if (recursive && !IsSpecialDir(f)) { WCHAR *d = path::Join(currDir, f); dirsToVisit.Append(d); } } else if (IsRegularFile(currFindData.dwFileAttributes)) { WCHAR *p = path::Join(currDir, f); currPath.Set(p); } BOOL hasMore = FindNextFile(currFindHandle, &currFindData); if (!hasMore) foundNext = TryNextDir(); } return currPath; }