在Boost Filesystem中过滤文件夹 [英] Filtering folders in Boost Filesystem
问题描述
我想通过Windows中的Boost Filesystem将所有文件保存在一个具体的文件夹(在本例中为Documents)中.
I want to get all the files inside a concrete folder (in this case Documents) with Boost Filesystem in Windows.
为此,我进行了筛选以确保没有文件夹.
For this I make a filter to assure I get no folders.
问题是,当我仍然获得以下文件夹时:
The problem is that when I still get the following folders :
"C:\....\Documents\My Videos",
"C:\....\Documents\My Music",
"C:\....\Documents\My Pictures"
(我没有其他文件夹)
代码如下:
boost::filesystem::directory_iterator end;
for (boost::filesystem::directory_iterator iter(documentsFolder); iter != end; ++iter){
if (!is_directory(*iter)){
std::cout << iter->path() << "\n";
}
else{
std::cout << "-------->>>" << iter->path() << "\n";
}
是否有任何方法可以在不进行手动过滤的情况下避免使用此文件夹?
Is there any way to avoid this folders without making a manual filtering?
非常感谢!
推荐答案
这些可能是连接点.
我只是通过使用 junction.exe
(来自sysinternals)在一个Windows框上进行尝试来建立连接点,并使用此小测试程序来精确定位它们:
I just tried it on a windows box by using junction.exe
(from sysinternals) to make a junction point, and using this little test program to pinpoint them:
#include <boost/range/iterator_range.hpp>
#include <boost/filesystem.hpp>
#include <vector>
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
using namespace boost::filesystem;
std::vector<std::string> args(argv+1, argv+argc);
if (args.empty()) args.push_back("C:\\WORK");
for (auto dir : args)
{
recursive_directory_iterator f(dir), l;
for (directory_entry& entry : boost::make_iterator_range(f,l))
{
if (is_other(entry))
{
assert(!is_regular_file(entry));
assert(!is_directory(entry));
std::cout << entry << "\n";
}
}
}
}
现在运行并打印出 test.exe"C:\ WORK"
"C:\WORK\MyJunction"
请注意, assers
证明不是普通文件也不是目录.
Note that the assers
prove that is neither a regular file nor a directory.
在一些不相关的新闻中,这对于目录迭代非常有用:
In slightly unrelated news, this is rather sweet for directory iteration:
int main(int argc, char* argv[])
{
using namespace boost::adaptors;
std::vector<std::string> const args(argv+1, argv+argc);
for (auto& dir : args)
for (auto& entry : traverse_directory(dir) | filtered(is_valid_file))
std::cout << entry << "\n";
}
使用这些非常简单的助手:
Using these rather simple helpers:
template <typename T>
boost::iterator_range<boost::filesystem::recursive_directory_iterator> traverse_directory(T const& dir) {
boost::filesystem::recursive_directory_iterator f(dir), l;
return boost::make_iterator_range(f, l);
}
static bool is_valid_file(boost::filesystem::directory_entry const& entry) {
return is_regular_file(entry) && !is_other(entry);
}
查看 在Coliru上直播
See it Live on Coliru
这篇关于在Boost Filesystem中过滤文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!