C++17 filesystem library recursive_directory_iterator

Viewed 17

I'm working on a C++ pgm to delete 0-length files and empty directories (those containing no files or subdirectories). Using C++17's filesystem library I have code like

string root(path);
vector<string> pathsFileEmpty; // complete paths to 0-length files
vector<string> pathsDirEmpty;  // complete paths to empty directories
auto iterOptions = fs::directory_options::follow_directory_symlink;
for(auto &e : fs::recursive_directory_iterator(path, iterOptions))
{
    auto path = e.path();
    if(!e.is_directory() && e.file_size() == 0)
    {
        pathsFileEmpty.push_back(path.string());
    }
    else if(e.is_directory())
    {
        auto d = fs::directory_iterator(path);
        if(begin(d) == end(d))
        {
            pathsDirEmpty.push_back(path.string());
        }
    }
}

The above code works correctly. Now I want to add the removal of the 0-length files and empty directories. The latter needs to be done recursively, along the complete path, back to the root if necessary.

My question is, will removing files and directories interfere with the recursive_directory_iterator? If so, I would have to delete the files and directories in multiple passes, which is much less efficient.

0 Answers
Related