Advertisement
Fruch

Untitled

Jun 7th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <string>
  2. #include <conio.h>
  3. #include <stdarg.h>
  4. #include <Windows.h>
  5. #define BUFFER_SIZE 2048
  6.  
  7. void searchAndPrint(wchar_t* templateStr, wchar_t* currDirStr, int level);
  8. void print(const wchar_t *format, ...);
  9. void offset(unsigned num);
  10. int main() {
  11.     wchar_t buffer[BUFFER_SIZE],
  12.         *currDir = 0;
  13.     size_t tmp;
  14.     DWORD dwLen;
  15.  
  16.     while (true)
  17.     {
  18.         dwLen = GetCurrentDirectoryW(0, currDir);
  19.         currDir = new wchar_t[dwLen];
  20.         GetCurrentDirectoryW(dwLen, currDir);
  21.         print(L"Текущая директория: %s\n", currDir);
  22.         print(L"Введите шаблон для поиска (\\quit для выхода): ");
  23.         _cgetws_s(buffer, &tmp);
  24.         if (wcscmp(buffer, L"\\quit") == 0)
  25.             break;
  26.         searchAndPrint(buffer, L".\\", 0);
  27.     }
  28.  
  29.     system("pause");
  30.     delete currDir;
  31.     return 0;
  32. }
  33.  
  34. void searchAndPrint(wchar_t* templateStr, wchar_t* currDirStr, int level)
  35. {
  36.     wchar_t *oldCurrDir = 0;
  37.     WIN32_FIND_DATA ffd;
  38.  
  39.     DWORD dwLen = GetCurrentDirectoryW(0, oldCurrDir);
  40.     oldCurrDir = new wchar_t[dwLen];
  41.     GetCurrentDirectoryW(dwLen, oldCurrDir);
  42.  
  43.     SetCurrentDirectory(currDirStr);
  44.  
  45.     HANDLE hSearch = FindFirstFile(templateStr, &ffd);
  46.  
  47.     offset(level);
  48.     print(L"<DIR>%s\n", currDirStr);
  49.     if (hSearch != INVALID_HANDLE_VALUE)
  50.     {
  51.         do
  52.         {
  53.             offset(level + 1);
  54.             print(L"%s\n", ffd.cFileName);
  55.         } while (FindNextFile(hSearch, &ffd));
  56.     }
  57.  
  58.     FindClose(hSearch);
  59.  
  60.     hSearch = FindFirstFile(L"?*", &ffd);
  61.     if (hSearch != INVALID_HANDLE_VALUE)
  62.     {
  63.         do
  64.         {
  65.             if (!(wcscmp(ffd.cFileName, L"..") == 0 ||
  66.                 wcscmp(ffd.cFileName, L".") == 0) &&
  67.                 ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  68.             {
  69.                 searchAndPrint(templateStr, ffd.cFileName, level + 1);
  70.             }
  71.         } while (FindNextFile(hSearch, &ffd));
  72.     }
  73.     FindClose(hSearch);
  74.     SetCurrentDirectory(oldCurrDir);
  75. }
  76.  
  77. void offset(unsigned num)
  78. {
  79.     for (unsigned i = 0; i < num; i++)
  80.         printf("--");
  81.     printf("- ");
  82. }
  83.  
  84. void print(const wchar_t *format, ...)
  85. {
  86.     wchar_t buffer[2048];
  87.     va_list args;
  88.     va_start(args, format);
  89.     wvsprintfW(buffer, format, args);
  90.     _cputws(buffer);
  91.     va_end(args);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement