Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <dirent.h>
  3. #include <vector>
  4. #include <sys/stat.h>
  5. using namespace std;
  6.  
  7. bool isdir(string path)
  8. {
  9.     struct stat sb;
  10.     if(stat(path.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode))
  11.         return true;
  12.     else return false;
  13. }
  14.  
  15. void lsdir(string path, bool recursiveListDir, bool searchHiddenFiles = false)
  16. {
  17.  
  18.     vector<string> files;
  19.     DIR *d;
  20.     struct dirent *dir;
  21.     d = opendir(path.c_str());
  22.     if(d)
  23.     {
  24.         auto cond = dir = readdir(d);
  25.  
  26.         while(cond != NULL)
  27.         {
  28.             string fn = dir->d_name;
  29.             if (!recursiveListDir)
  30.             {
  31.                 if(!searchHiddenFiles)
  32.                 {
  33.                     if(fn[0] != '.')
  34.                         files.push_back(fn);
  35.                 }
  36.                 else files.push_back(fn);
  37.             }
  38.             else
  39.             {
  40.  
  41.                 if(!searchHiddenFiles)
  42.                 {
  43.                     if(isdir(fn))
  44.                         if(fn[0] != '.')
  45.                             files.push_back(fn);
  46.                 }
  47.                 else
  48.                     if(isdir(fn)) files.push_back(fn);
  49.             }
  50.         }
  51.         closedir(d);
  52.     }
  53.  
  54.     if (recursiveListDir)
  55.     {
  56.         for(long unsigned int recursiveCounter = 0;
  57.                 recursiveCounter < files.size(); recursiveCounter++)
  58.             lsdir(files[recursiveCounter], false, searchHiddenFiles);
  59.     }
  60.     else for(int filesIterator = 0; filesIterator < files.size();
  61.             filesIterator++) cout << files[filesIterator];
  62.  
  63. }
  64.  
  65. int main()
  66. {
  67.     string pathToSearch = ".";
  68.  
  69.     lsdir(pathToSearch, false);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement