c++c++17std-filesystem

fs::recursive_directory_iterator command doesn't iterate over sub-directories under a symbolic link


I found that the toy implementation of recursive_directory_iterator furnished below doesn't iterate over sub-directories under symbolic links. So the sub-directories under a symbolic link folder are skipped. How can the issue be addressed?

void ScanMediaFolders()
{
    root_path = "D:\\Projects\\Media";
    for (const directory_entry& dir_entry :
        recursive_directory_iterator(root_path)){
        std::cout << dir_entry << '\n';
    }

}

int main() {
    ScanMediaFolders();
    return 0;
}

Solution

  • recursive_directory_iterator has (since C++17) a constructor (number 4):

    recursive_directory_iterator(
        const std::filesystem::path& p,
        std::filesystem::directory_options options );
    

    directory_options is an enum, and one of its entries is: follow_directory_symlink, which has the following meaning:

    Follow rather than skip directory symlinks.

    Therefore you need to replace:

    recursive_directory_iterator(root_path)
    

    with:

    recursive_directory_iterator(root_path, 
                         std::filesystem::directory_options::follow_directory_symlink)