Advertisement
Guest User

Untitled

a guest
Jun 12th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <Windows.h>
  4.  
  5. using namespace std;
  6.  
  7. static vector<wstring> FindFiles(const wstring& searchPath, DWORD fileAttributeMask, DWORD fileAttributeNotMask)
  8. {
  9.     vector<wstring> result;
  10.     WIN32_FIND_DATA findData;
  11.     DWORD lastError;
  12.     bool findNextFileFailed = false;
  13.  
  14.     auto searchOperation = (fileAttributeMask & FILE_ATTRIBUTE_DIRECTORY) ? FindExSearchLimitToDirectories : FindExSearchNameMatch;
  15.     auto searchHandle = FindFirstFileEx(searchPath.c_str(), FindExInfoStandard, &findData, searchOperation, nullptr, 0);
  16.     if (searchHandle == INVALID_HANDLE_VALUE) return result;
  17.  
  18.     do
  19.     {
  20.         if (_wcsicmp(findData.cFileName, L".") != 0 && _wcsicmp(findData.cFileName, L"..") != 0)
  21.         {
  22.             if ((findData.dwFileAttributes & fileAttributeMask) && (findData.dwFileAttributes & fileAttributeNotMask) == 0)
  23.             {
  24.                 result.push_back(findData.cFileName);
  25.                
  26.                 if (findNextFileFailed)
  27.                 {
  28.                     // If this gets hit, that means FindNextFile failed at least once
  29.                     // before succeeding and returning a file
  30.                     __debugbreak();
  31.                 }
  32.             }
  33.         }
  34.  
  35.         FindNextFile(searchHandle, &findData);
  36.  
  37.         lastError = GetLastError();
  38.         if (lastError != ERROR_SUCCESS && lastError != ERROR_NO_MORE_FILES)
  39.         {
  40.             findNextFileFailed = true;
  41.             OutputDebugString((L"Something went bad while enumerating \"" + searchPath + L"\": " + to_wstring(GetLastError()) + L"\r\n").c_str());
  42.         }
  43.     }
  44.     while (lastError != ERROR_NO_MORE_FILES);
  45.  
  46.     FindClose(searchHandle);
  47.     SetLastError(ERROR_SUCCESS);
  48.  
  49.     return result;
  50. }
  51.  
  52. vector<wstring> GetFilesInDirectory(wstring path, const wstring& searchPattern, bool recursive)
  53. {
  54.     vector<wstring> result;
  55.  
  56.     if (path[path.length() - 1] != L'\\')
  57.     {
  58.         path += L'\\';
  59.     }
  60.  
  61.     // Find folders
  62.     if (recursive)
  63.     {
  64.         for (const auto& directory : FindFiles(path + L"*.*", FILE_ATTRIBUTE_DIRECTORY, 0))
  65.         {
  66.             for (auto& file : GetFilesInDirectory(path + directory, searchPattern, recursive))
  67.             {
  68.                 result.emplace_back(std::move(file));
  69.             }
  70.         }
  71.     }
  72.  
  73.     // Find files
  74.     for (const auto& fileName : FindFiles(path + searchPattern, 0xFFFFFFFF, FILE_ATTRIBUTE_DIRECTORY))
  75.     {
  76.         result.emplace_back(path + fileName);
  77.     }
  78.  
  79.     return result;
  80. }
  81.  
  82. int main()
  83. {
  84.     auto files = GetFilesInDirectory(L"C:\\Windows\\System32", L"*.*", true);
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement