如何使用boost删除目录中的空文件夹 [英] how to remove empty folders in a directory using boost
本文介绍了如何使用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;
}
}
这篇关于如何使用boost删除目录中的空文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文