1

I'm trying to list all the directories and files from the computer and want to work with the DIR and FILES.

Note:

I'm on Windows platform. Using Visual Studio 2019 Community edition. And FYI running the compiled binary as Administrator because of the permission issues on windows. Please do run as Administrator to reproduce the error.

Here I'm using recursive_directory_iterator from the <filesystem> library which now comes in c++17 standard.

Here's a basic code snippet:

#include <iostream>
#include <windows.h>
#include <vector>
#include <filesystem>


namespace fs = std::filesystem;
using namespace std;

string f_path = "E:\\";


void func1(string file_path)
{

    vector<string> my_paths{};

        for (auto& p : fs::recursive_directory_iterator(file_path))
        {
            cout << p.path() << endl;

        }


}


int main()
{


    func1(f_path);
    return 0;


}

This is the error this code throws.

enter image description here

I'm new to Cpp and not able to solve this. I appreciate your efforts on taking your time and helping me.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
hitbox
  • 39
  • 2
  • 1
    Look at the section **Exceptions** in [recursive_directory_iterator& operator++();](https://en.cppreference.com/w/cpp/filesystem/recursive_directory_iterator/increment) and try to catch `std::filesystem_error`. – 273K Jan 20 '20 at 15:36
  • @S.M. can you help me on this? I'm new to cpp and not able to find the correct way to do it. – hitbox Jan 20 '20 at 15:41
  • Place your for-loop inside [try-block](https://en.cppreference.com/w/cpp/language/try_catch). – 273K Jan 20 '20 at 15:44
  • @S.M. i just did this and it says at the directory: "E:\\System Volume Information" recursive_directory_iterator::operator++: Access Denied." can you help me to skip such 'Access denied' directories and continue to other DIRs and FILES? – hitbox Jan 20 '20 at 15:50
  • 1
    What is in your `E:` root directory? If you look at your output, you see some files have been processed, and a mystery file is where the `abort()` occurs. – PaulMcKenzie Jan 20 '20 at 15:50
  • 1
    @PaulMcKenzie i try catch the error and it says 'Access Denied' at the directory: E:\\System Volume Information" even after running as administrator. Can you help me jump such access denied DIRS and FILES if encountered and continue scanning other files and dirs? – hitbox Jan 20 '20 at 15:53
  • But your last comment shows you how to keep processing files. You caught the exception, you see it is an "Access Denied", so the iterator just increments and keeps going, no? Before, it seems you were not catching the exception, and instead the compiler's default handler took over (which is to call `abort`). – PaulMcKenzie Jan 20 '20 at 16:06
  • For me it did not continue because I put the try { for ...} catch ( std::exception & exc ) {} – drescherjm Jan 20 '20 at 16:08
  • Maybe this is related: [https://stackoverflow.com/a/16101173/487892](https://stackoverflow.com/a/16101173/487892) – drescherjm Jan 20 '20 at 16:09

1 Answers1

1

To skip "Access Denied" errors, you should use the alternative loop and std::filesystem::recursive_directory_iterator::increment instead of std::filesystem::recursive_directory_iterator::operator++. It is described in the Exceptions section of std::filesystem::recursive_directory_iterator::operator++, std::filesystem::recursive_directory_iterator::increment. Something like bellow (written without compiling and testing)

void func1(const string& file_path) {
    std::error_code os_error;
    fs::recursive_directory_iterator walker(file_path);
    for (auto i = fs::begin(walker); i != fs::end(walker); i = i.increment(os_error)) {
        if (!os_error)
            std::cout << i->path() << std::endl;
    }
}
273K
  • 29,503
  • 10
  • 41
  • 64
  • Now it did not have the exception but iteration stopped at the place the other method would throw. Here I am talking about running it under windows on a non OS drive in regular user mode ( not run as admin or elevated). In this case for me it shows 2 folders in the recycle bin and stops without a crash or error with the new code. – drescherjm Jan 20 '20 at 16:47
  • 1
    @S.M. it shows me two files in RecycleBIN and stops. And running the compiled binary for this function and running it as Admin throws the same run time error. – hitbox Jan 20 '20 at 17:01
  • 2
    guys i have found this parameter options fs::directory_options::skip_permission_denied can this help? – hitbox Jan 20 '20 at 19:13
  • 2
    Now that worked for me. changing your original for () loop to `for (auto& p : fs::recursive_directory_iterator(file_path, fs::directory_options::skip_permission_denied))` @hitbox you may want to answer your own question – drescherjm Jan 20 '20 at 20:55