递归目录迭代器分步删除问题

时间:2018-11-27 10:04:04

标签: c++

1)文件形状

C:\\FTP\\2\\3\\99\\100  There's a txt in 99.
C:\\FTP\\2\\5\\11\\
C:\\FTP\\2\\5\\23\\.txt
C:\\FTP\\2\\.txt

2)代码

void OldDirectoryCleaner::CleanOldFilesRecursive(boost::filesystem::path TopPath)
{
try
{
    LogS << "Scanning " << TopPath.generic_string() << " >> ";
    if (boost::filesystem::exists(TopPath))
    {
        boost::filesystem::recursive_directory_iterator PathIter{ TopPath };

        while (PathIter != boost::filesystem::recursive_directory_iterator{} && *PathIter !=TopPath)
        {
            boost::filesystem::path RemovePath = boost::filesystem::path(*PathIter);
            bool RemoveSuccess = false;
            try
            {
                if (boost::filesystem::is_directory(PathIter->status()))
                {
                    if (boost::filesystem::is_empty(*PathIter))
                    {
                        try
                        {
                            LogS << " Deleting empty old folder " << RemovePath.generic_string();
                            boost::filesystem::remove(RemovePath);
                            RemoveSuccess = true;
                            LogS << "Success. ";                        
                        }
                        catch (...)
                        {
                            LogS << "Error. ";
                        }
                    }

                }

            }
            catch (...) {}

            if (!RemoveSuccess)
            {
                PathIter++;                                                 
            }

            else 
            {   
                boost::filesystem::path RemovePath = boost::filesystem::path(*PathIter);
                PathIter = boost::filesystem::recursive_directory_iterator{ RemovePath.parent_path() };     
                boost::filesystem::path x = RemovePath.parent_path();

            }

3)输出步骤

RemovePath  C:\\FTP\\2  
RemovePath  C:\\FTP\\2\\3
RemovePath  C:\\FTP\\2\\3\\99
RemovePath  C:\\FTP\\2\\3\\99\\100 deleted.
RemovePath  C:\\FTP\\2\\3\\99\\.txt" } after this It's getting out of the loop.

为什么不检查并删除其他空文件?

0 个答案:

没有答案