如何使用boost删除目录中的空文件夹
问题描述:
如何删除目录中的空文件夹。 boost :: filesystem :: recursive_directory_iterator任何人都可以帮助我
how to remove empty folders in a directory. boost::filesystem::recursive_directory_iterator can any one help me
for(boost::filesystem::recursive_directory_iterator it(musicPaths); it != boost::filesystem::recursive_directory_iterator(); ++it)
{
if ( is_directory(it->status()) )
{
//
boost::filesystem::path pp = it->path();
if( is_empty( pp))
{
// path removePath=it->path().filename();
//string str = pp.string();
bool bres=boost::filesystem3::remove(pp);
// boost::filesystem::current_path=pp.current_path();
//pp=pp.parent_path();
}
}
}
应用程序崩溃。
the application crash.
答
我在 http:/上发了一篇论坛帖子([Filesystem] remove和recursive_directory_iterator) /news.gmane.org/gmane.comp.lib.boost.user [ ^ ]。不确定它是否是功能。将for循环转换为while并在删除之前增加迭代器对我来说是个窍门:
I made a forum post ([Filesystem] remove and recursive_directory_iterator) on http://news.gmane.org/gmane.comp.lib.boost.user[^]. Not sure if it is a 'feature'. Turning the for loop into while and increment iterator before remove does the trick for me:
fs::recursive_directory_iterator it(pthDirectory);
fs::recursive_directory_iterator itEnd;
while (it != itEnd)
{
boost::system::error_code ec;
const fs::path& rPath = it->path();
if (fs::is_directory(rPath, ec) && fs::is_empty(rPath, ec))
{
const fs::path pth = rPath;
++it;
fs::remove(pth, ec);
}
else
{
++it;
}
}