Home > Net >  How to know if sub directory exist in c ?
How to know if sub directory exist in c ?

Time:05-29

I want to stack directory until directory hits max-depth.
Therefore, I tried to use fs::filesystem.
At first, I approach by depth() like

 for (auto itr = fs::recursive_directory_iterator(fs::current_path()/path); itr != fs::recursive_directory_iterator(); itr  )
        {
           
             (itr.depth() == ???) 
                
            }

But Since I don't know max-depth. I failed. how can I know if directory has sub-directory or not?

CodePudding user response:

Try this snippet to find max-depth in the current directory:

    int max_depth = 0;
    // use as first pass    
    for(auto itr = filesystem::recursive_directory_iterator(filesystem::current_path()); itr != filesystem::recursive_directory_iterator(); itr  )
    {
        if (is_directory(itr->path())) {
            if (itr.depth() > max_depth) max_depth = itr.depth();
        }
    }
  • Related