recursion - c++ 17 filesystem(experimental/filesystem at Windows) remove all empty directories recursively -


i'm trying write c++ script uses c++17 filesystem library recursively remove empty directories found @ given root directory(to make things clearer i'll use cwd in code sample).
so, have following directory structure: c:\users\anrob\pictures\sonya100\2017\august\week1\8-1-2017 , know in path c:\users\anrob\pictures\sonya100\2017\august\week1\8-1-2017 empty, c:\users\anrob\pictures\sonya100\2017\august\week1 has nothing 8-1-2017 empty directory, , c:\users\anrob\pictures\sonya100\2017\august\ have nothing empty week1 directory once remove c:\users\anrob\pictures\sonya100\2017\august\week1\8-1-2017.
here current code(note i'm using microsoft (r) c/c++ optimizing compiler version 19.00.24225.1 x64):

#include <experimental/filesystem>   int main() {     std::experimental::filesystem::path top =  std::experimental::filesystem::current_path(),      dir_path;     (auto& p:  std::experimental::filesystem::recursive_directory_iterator(top)) {         dir_path = p.path();         while (std::experimental::filesystem::is_directory(dir_path) &&          std::experimental::filesystem::is_empty(dir_path)) {             std::experimental::filesystem::remove(dir_path);             dir_path = dir_path.parent_path();         }     }     return 0; } 

when compile , run code, removes 1 empty directory(i.e., c:\users\anrob\pictures\sonya100\2017\august\week1\8-1-2017, , not of soon-to-be empty directories.

so, decided try 1 more time copying paths vector of paths, @benjaminlindley suggested.
didn't work, once again...
decided take deep @ the filesystem library documentation.
after thorough reading of documentation, realised should using canonical paths , should iterate on vector of canonical paths reverse iterator.
script working charm, "recursively" removing empty directories expected.
think it's idea share solution here, since couldn't find clear answer earlier question.
appreciate if stack overflow community upvote answer , question, because don't think i'm going person ever stumble on problem.
lastly, here's solution problem:

#include <experimental/filesystem>   #include <vector> int main() {     std::experimental::filesystem::path top = std::experimental::filesystem::current_path();     std::experimental::filesystem::path dir_path;     std::vector<std::experimental::filesystem::path> directories;     (auto& p: std::experimental::filesystem::recursive_directory_iterator(top)) {         dir_path = p.path();         if (std::experimental::filesystem::is_directory(dir_path)) {             directories.push_back(std::experimental::filesystem::canonical(dir_path));         }     }     (std::vector<std::experimental::filesystem::path>::reverse_iterator rit = directories.rbegin(); rit != directories.rend(); ++rit) {         if (std::experimental::filesystem::is_empty(*rit)) {             std::experimental::filesystem::remove(*rit);         }     }     return 0; } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -