我可以使用掩码通过 Boost 迭代目录中的文件吗?

发布于 2024-07-30 05:28:08 字数 157 浏览 5 评论 0原文

我想迭代目录中与 somefiles*.txt 等内容匹配的所有文件。

boost::filesystem 是否有内置的东西可以做到这一点,或者我是否需要针对每个 leaf() 的正则表达式或其他东西?

I want to iterate over all files in a directory matching something like somefiles*.txt.

Does boost::filesystem have something built in to do that, or do I need a regex or something against each leaf()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

乄_柒ぐ汐 2024-08-06 05:28:09

我相信directory_iterators只会提供目录中的所有文件。 您可以根据需要过滤它们。

I believe the directory_iterators will only provide all files in a directory. It up to you to filter them as necessary.

酒绊 2024-08-06 05:28:09

即使当我使用 i->path().extension() 而不是 leaf() 时,接受的答案也无法为我编译。 对我有用的是此网站中的示例。 这是修改后的代码,用于应用过滤器:

vector<string> results;
filesystem::path filepath(fullpath_to_file);
filesystem::directory_iterator it(filepath);
filesystem::directory_iterator end;
const boost::regex filter("myfilter(capturing group)");
BOOST_FOREACH(filesystem::path const &p, make_pair(it, end))
{
     if(is_regular_File(p))
     {
          match_results<string::const_iterator> what;
          if (regex_search(it->path().filename().string(), what, pidFileFilter, match_default))
          {
               string res = what[1];
               results.push_back(res);
          }
     }
}

我使用的是 Boost 版本:1.53.0。

为什么我们不都只使用 glob() 和一些正则表达式超出了我的范围。

The accepted answer did not compile for me even when I used i->path().extension() instead of leaf(). What did work for me was an example from this website. Here's the code, modified, to apply a filter:

vector<string> results;
filesystem::path filepath(fullpath_to_file);
filesystem::directory_iterator it(filepath);
filesystem::directory_iterator end;
const boost::regex filter("myfilter(capturing group)");
BOOST_FOREACH(filesystem::path const &p, make_pair(it, end))
{
     if(is_regular_File(p))
     {
          match_results<string::const_iterator> what;
          if (regex_search(it->path().filename().string(), what, pidFileFilter, match_default))
          {
               string res = what[1];
               results.push_back(res);
          }
     }
}

I'm using Boost version: 1.53.0.

Why we don't all just use glob() and some regex is beyond me.

对你再特殊 2024-08-06 05:28:09

正如 Julien-L 的文章 QDir 的末尾所提到的,正是您想要的想要。
https://qt-project.org/doc/ qt-5.0/qtcore/qdir.html#QDir-3

As mentioned at the end of Julien-L's post QDir is exactly what you want.
https://qt-project.org/doc/qt-5.0/qtcore/qdir.html#QDir-3

抱猫软卧 2024-08-06 05:28:09

我之前一直在寻找解决方案,我认为我的解决方案是最简单的

#include <boost/filesystem.hpp>
#include <boost/regex.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/exception/all.hpp>

struct dir_filter_iter
        : public boost::iterator_facade<
                dir_filter_iter,
                boost::filesystem::path,
                boost::forward_traversal_tag,
                boost::filesystem::path
        >
{
        using path = boost::filesystem::path;
        using impl_type = boost::filesystem::directory_iterator;

        dir_filter_iter():impl_(){}
        dir_filter_iter(path p, boost::regex rgx):impl_(std::move(p)),rgx_(std::move(rgx)){
                namespace bf = boost::filesystem;
                if( ! bf::is_directory(p) ){
                        BOOST_THROW_EXCEPTION(
                                boost::enable_error_info(std::domain_error("not a dir"))
                                << boost::errinfo_file_name(p.string()));
                }

             //advance to first matching item if impl_ is not already at end()
             if(impl_ != impl_type()) {
                  std::string s(impl_->path().string());
                  if( !boost::regex_match( s, rgx_ )) increment();
             }
        }
        private:
        friend class boost::iterator_core_access;
        bool equal(const dir_filter_iter& that)const{
                return this->impl_ == that.impl_;
        }
        void increment(){
                assert( impl_ != impl_type() );
                for(;;){
                        ++impl_;
                        if( impl_ == impl_type() )
                                break;
                        std::string s(impl_->path().string());
                        if( boost::regex_match( s, rgx_ ) ){
                                break;
                        }
                }
        }
        path dereference()const{
                assert( impl_ != impl_type() );
                return *impl_;
        }
        impl_type impl_;
        boost::regex rgx_;
};
struct dir_filter_iter_maker{
        using value_type = dir_filter_iter;

        explicit dir_filter_iter_maker(boost::regex rgx):rgx_(rgx){}

        value_type make()const{
                return value_type();
        }
        value_type make(boost::filesystem::path p)const{
                return value_type(std::move(p),rgx_);
        }
        template<typename... Args>
        auto operator()(Args&&... args)->decltype(make(args...)){
                return this->make(std::forward<Args>(args)...);
        }
        private:
        boost::regex rgx_;
};

那么你可以这样做

    dir_filter_iter_maker di_maker(boost::regex(R"_(.*\.hpp)_"));
    std::for_each( di_maker(p), di_maker(), [](const bf::path& p){std::cout << p.string() << "\n";});

I was looking for a solution to this earlier and I think my solution is the simplest

#include <boost/filesystem.hpp>
#include <boost/regex.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/exception/all.hpp>

struct dir_filter_iter
        : public boost::iterator_facade<
                dir_filter_iter,
                boost::filesystem::path,
                boost::forward_traversal_tag,
                boost::filesystem::path
        >
{
        using path = boost::filesystem::path;
        using impl_type = boost::filesystem::directory_iterator;

        dir_filter_iter():impl_(){}
        dir_filter_iter(path p, boost::regex rgx):impl_(std::move(p)),rgx_(std::move(rgx)){
                namespace bf = boost::filesystem;
                if( ! bf::is_directory(p) ){
                        BOOST_THROW_EXCEPTION(
                                boost::enable_error_info(std::domain_error("not a dir"))
                                << boost::errinfo_file_name(p.string()));
                }

             //advance to first matching item if impl_ is not already at end()
             if(impl_ != impl_type()) {
                  std::string s(impl_->path().string());
                  if( !boost::regex_match( s, rgx_ )) increment();
             }
        }
        private:
        friend class boost::iterator_core_access;
        bool equal(const dir_filter_iter& that)const{
                return this->impl_ == that.impl_;
        }
        void increment(){
                assert( impl_ != impl_type() );
                for(;;){
                        ++impl_;
                        if( impl_ == impl_type() )
                                break;
                        std::string s(impl_->path().string());
                        if( boost::regex_match( s, rgx_ ) ){
                                break;
                        }
                }
        }
        path dereference()const{
                assert( impl_ != impl_type() );
                return *impl_;
        }
        impl_type impl_;
        boost::regex rgx_;
};
struct dir_filter_iter_maker{
        using value_type = dir_filter_iter;

        explicit dir_filter_iter_maker(boost::regex rgx):rgx_(rgx){}

        value_type make()const{
                return value_type();
        }
        value_type make(boost::filesystem::path p)const{
                return value_type(std::move(p),rgx_);
        }
        template<typename... Args>
        auto operator()(Args&&... args)->decltype(make(args...)){
                return this->make(std::forward<Args>(args)...);
        }
        private:
        boost::regex rgx_;
};

Then you can do

    dir_filter_iter_maker di_maker(boost::regex(R"_(.*\.hpp)_"));
    std::for_each( di_maker(p), di_maker(), [](const bf::path& p){std::cout << p.string() << "\n";});
江南月 2024-08-06 05:28:08

编辑:如注释中所述,下面的代码对于 v3 之前的 boost::filesystem 版本有效。 对于v3,请参考评论中的建议。


boost::filesystem 没有通配符搜索,你必须自己过滤文件。

这是一个使用 boost::filesystemdirectory_iterator 提取目录内容并使用 boost::regex 进行过滤的代码示例:

const std::string target_path( "/my/directory/" );
const boost::regex my_filter( "somefiles.*\.txt" );

std::vector< std::string > all_matching_files;

boost::filesystem::directory_iterator end_itr; // Default ctor yields past-the-end
for( boost::filesystem::directory_iterator i( target_path ); i != end_itr; ++i )
{
    // Skip if not a file
    if( !boost::filesystem::is_regular_file( i->status() ) ) continue;

    boost::smatch what;

    // Skip if no match for V2:
    if( !boost::regex_match( i->leaf(), what, my_filter ) ) continue;
    // For V3:
    //if( !boost::regex_match( i->path().filename().string(), what, my_filter ) ) continue;

    // File matches, store it
    all_matching_files.push_back( i->leaf() );
}

(如果您正在寻找具有内置目录过滤功能的即用类,请查看 Qt 的 QDir。)

EDIT: As noted in the comments, the code below is valid for versions of boost::filesystem prior to v3. For v3, refer to the suggestions in the comments.


boost::filesystem does not have wildcard search, you have to filter files yourself.

This is a code sample extracting the content of a directory with a boost::filesystem's directory_iterator and filtering it with boost::regex:

const std::string target_path( "/my/directory/" );
const boost::regex my_filter( "somefiles.*\.txt" );

std::vector< std::string > all_matching_files;

boost::filesystem::directory_iterator end_itr; // Default ctor yields past-the-end
for( boost::filesystem::directory_iterator i( target_path ); i != end_itr; ++i )
{
    // Skip if not a file
    if( !boost::filesystem::is_regular_file( i->status() ) ) continue;

    boost::smatch what;

    // Skip if no match for V2:
    if( !boost::regex_match( i->leaf(), what, my_filter ) ) continue;
    // For V3:
    //if( !boost::regex_match( i->path().filename().string(), what, my_filter ) ) continue;

    // File matches, store it
    all_matching_files.push_back( i->leaf() );
}

(If you are looking for a ready-to-use class with builtin directory filtering, have a look at Qt's QDir.)

怪我闹别瞎闹 2024-08-06 05:28:08

有一个 Boost范围适配器方式:

#define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
#include <boost/filesystem.hpp>
#include <boost/range/adaptors.hpp>

namespace bfs = boost::filesystem;
namespace ba = boost::adaptors;

const std::string target_path( "/my/directory/" );
const boost::regex my_filter( "somefiles.*\.txt" );
boost::smatch what;

for (auto &entry: boost::make_iterator_range(bfs::directory_iterator(target_path), {})
| ba::filtered(static_cast<bool (*)(const bfs::path &)>(&bfs::is_regular_file))
| ba::filtered([&](const bfs::path &path){ return boost::regex_match(path.filename().string(), what, my_filter); })
)
{
  // There are only files matching defined pattern "somefiles*.txt".
  std::cout << entry.path().filename() << std::endl;
}

There is a Boost Range Adaptors way:

#define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
#include <boost/filesystem.hpp>
#include <boost/range/adaptors.hpp>

namespace bfs = boost::filesystem;
namespace ba = boost::adaptors;

const std::string target_path( "/my/directory/" );
const boost::regex my_filter( "somefiles.*\.txt" );
boost::smatch what;

for (auto &entry: boost::make_iterator_range(bfs::directory_iterator(target_path), {})
| ba::filtered(static_cast<bool (*)(const bfs::path &)>(&bfs::is_regular_file))
| ba::filtered([&](const bfs::path &path){ return boost::regex_match(path.filename().string(), what, my_filter); })
)
{
  // There are only files matching defined pattern "somefiles*.txt".
  std::cout << entry.path().filename() << std::endl;
}
美人迟暮 2024-08-06 05:28:08

我的解决方案本质上与 Julien-L 相同,但封装在包含文件中,使用起来更好。 使用 boost::filesystem v3 实现。 我猜想类似的东西不会直接包含在 boost::filesystem 中,因为它会引入对 boost::regex 的依赖。

#include "FilteredDirectoryIterator.h"
std::vector< std::string > all_matching_files;
std::for_each(
        FilteredDirectoryIterator("/my/directory","somefiles.*\.txt"),
        FilteredDirectoryIterator(),
        [&all_matching_files](const FilteredDirectoryIterator::value_type &dirEntry){
                all_matching_files.push_back(dirEntry.path());
            }
        );

或者使用 FilteredRecursiveDirectoryIterator 进行递归子目录搜索:

#include "FilteredDirectoryIterator.h"
std::vector< std::string > all_matching_files;
std::for_each(
        FilteredRecursiveDirectoryIterator("/my/directory","somefiles.*\.txt"),
        FilteredRecursiveDirectoryIterator(),
        [&all_matching_files](const FilteredRecursiveDirectoryIterator::value_type &dirEntry){
                all_matching_files.push_back(dirEntry.path());
            }
        );

FilteredDirectoryIterator.h

#ifndef TOOLS_BOOST_FILESYSTEM_FILTEREDDIRECTORYITERATOR_H_
#define TOOLS_BOOST_FILESYSTEM_FILTEREDDIRECTORYITERATOR_H_

#include "boost/filesystem.hpp"
#include "boost/regex.hpp"
#include <functional>

template <class NonFilteredIterator = boost::filesystem::directory_iterator>
class FilteredDirectoryIteratorTmpl
:   public std::iterator<
    std::input_iterator_tag, typename NonFilteredIterator::value_type
    >
{
private:
    typedef std::string string;
    typedef boost::filesystem::path path;
    typedef
        std::function<
            bool(const typename NonFilteredIterator::value_type &dirEntry)
            >
        FilterFunction;

    NonFilteredIterator it;

    NonFilteredIterator end;

    const FilterFunction filter;

public:

    FilteredDirectoryIteratorTmpl();

    FilteredDirectoryIteratorTmpl(
        const path &iteratedDir, const string ®exMask
        );

    FilteredDirectoryIteratorTmpl(
        const path &iteratedDir, const boost::regex &mask
        );

    FilteredDirectoryIteratorTmpl(
        const path &iteratedDir,
        const FilterFunction &filter
        );

    //preincrement
    FilteredDirectoryIteratorTmpl<NonFilteredIterator>& operator++() {
        for(++it;it!=end && !filter(*it);++it);
        return *this;
    };

    //postincrement
    FilteredDirectoryIteratorTmpl<NonFilteredIterator> operator++(int) {
        for(++it;it!=end && !filter(*it);++it);
        return FilteredDirectoryIteratorTmpl<NonFilteredIterator>(it,filter);
    };
    const boost::filesystem::directory_entry &operator*() {return *it;};
    bool operator!=(const FilteredDirectoryIteratorTmpl<NonFilteredIterator>& other)
    {
        return it!=other.it;
    };
    bool operator==(const FilteredDirectoryIteratorTmpl<NonFilteredIterator>& other)
    {
        return it==other.it;
    };
};

typedef
    FilteredDirectoryIteratorTmpl<boost::filesystem::directory_iterator>
    FilteredDirectoryIterator;

typedef
    FilteredDirectoryIteratorTmpl<boost::filesystem::recursive_directory_iterator>
    FilteredRecursiveDirectoryIterator;

template <class NonFilteredIterator>
FilteredDirectoryIteratorTmpl<NonFilteredIterator>::FilteredDirectoryIteratorTmpl()
:   it(),
    filter(
        [](const boost::filesystem::directory_entry& /*dirEntry*/){return true;}
        )
{

}

template <class NonFilteredIterator>
FilteredDirectoryIteratorTmpl<NonFilteredIterator>::FilteredDirectoryIteratorTmpl(
    const path &iteratedDir,const string ®exMask
    )
:   FilteredDirectoryIteratorTmpl(iteratedDir, boost::regex(regexMask))
{
}

template <class NonFilteredIterator>
FilteredDirectoryIteratorTmpl<NonFilteredIterator>::FilteredDirectoryIteratorTmpl(
    const path &iteratedDir,const boost::regex ®exMask
    )
:   it(NonFilteredIterator(iteratedDir)),
    filter(
        [regexMask](const boost::filesystem::directory_entry& dirEntry){
            using std::endl;
            // return false to skip dirEntry if no match
            const string filename = dirEntry.path().filename().native();
            return boost::regex_match(filename, regexMask);
        }
        )
{
    if (it!=end && !filter(*it)) ++(*this);
}

template <class NonFilteredIterator>
FilteredDirectoryIteratorTmpl<NonFilteredIterator>::FilteredDirectoryIteratorTmpl(
    const path &iteratedDir, const FilterFunction &filter
    )
:   it(NonFilteredIterator(iteratedDir)),
    filter(filter)
{
    if (it!=end && !filter(*it)) ++(*this);
}

#endif

My solution is essentially the same as Julien-L, but encapsulated in the include file it is nicer to use. Implemented using boost::filesystem v3. I guess that something like that is not included in the boost::filesystem directly because it would introduce dependency on boost::regex.

#include "FilteredDirectoryIterator.h"
std::vector< std::string > all_matching_files;
std::for_each(
        FilteredDirectoryIterator("/my/directory","somefiles.*\.txt"),
        FilteredDirectoryIterator(),
        [&all_matching_files](const FilteredDirectoryIterator::value_type &dirEntry){
                all_matching_files.push_back(dirEntry.path());
            }
        );

alternatively use FilteredRecursiveDirectoryIterator for recursive sub directories search:

#include "FilteredDirectoryIterator.h"
std::vector< std::string > all_matching_files;
std::for_each(
        FilteredRecursiveDirectoryIterator("/my/directory","somefiles.*\.txt"),
        FilteredRecursiveDirectoryIterator(),
        [&all_matching_files](const FilteredRecursiveDirectoryIterator::value_type &dirEntry){
                all_matching_files.push_back(dirEntry.path());
            }
        );

FilteredDirectoryIterator.h

#ifndef TOOLS_BOOST_FILESYSTEM_FILTEREDDIRECTORYITERATOR_H_
#define TOOLS_BOOST_FILESYSTEM_FILTEREDDIRECTORYITERATOR_H_

#include "boost/filesystem.hpp"
#include "boost/regex.hpp"
#include <functional>

template <class NonFilteredIterator = boost::filesystem::directory_iterator>
class FilteredDirectoryIteratorTmpl
:   public std::iterator<
    std::input_iterator_tag, typename NonFilteredIterator::value_type
    >
{
private:
    typedef std::string string;
    typedef boost::filesystem::path path;
    typedef
        std::function<
            bool(const typename NonFilteredIterator::value_type &dirEntry)
            >
        FilterFunction;

    NonFilteredIterator it;

    NonFilteredIterator end;

    const FilterFunction filter;

public:

    FilteredDirectoryIteratorTmpl();

    FilteredDirectoryIteratorTmpl(
        const path &iteratedDir, const string ®exMask
        );

    FilteredDirectoryIteratorTmpl(
        const path &iteratedDir, const boost::regex &mask
        );

    FilteredDirectoryIteratorTmpl(
        const path &iteratedDir,
        const FilterFunction &filter
        );

    //preincrement
    FilteredDirectoryIteratorTmpl<NonFilteredIterator>& operator++() {
        for(++it;it!=end && !filter(*it);++it);
        return *this;
    };

    //postincrement
    FilteredDirectoryIteratorTmpl<NonFilteredIterator> operator++(int) {
        for(++it;it!=end && !filter(*it);++it);
        return FilteredDirectoryIteratorTmpl<NonFilteredIterator>(it,filter);
    };
    const boost::filesystem::directory_entry &operator*() {return *it;};
    bool operator!=(const FilteredDirectoryIteratorTmpl<NonFilteredIterator>& other)
    {
        return it!=other.it;
    };
    bool operator==(const FilteredDirectoryIteratorTmpl<NonFilteredIterator>& other)
    {
        return it==other.it;
    };
};

typedef
    FilteredDirectoryIteratorTmpl<boost::filesystem::directory_iterator>
    FilteredDirectoryIterator;

typedef
    FilteredDirectoryIteratorTmpl<boost::filesystem::recursive_directory_iterator>
    FilteredRecursiveDirectoryIterator;

template <class NonFilteredIterator>
FilteredDirectoryIteratorTmpl<NonFilteredIterator>::FilteredDirectoryIteratorTmpl()
:   it(),
    filter(
        [](const boost::filesystem::directory_entry& /*dirEntry*/){return true;}
        )
{

}

template <class NonFilteredIterator>
FilteredDirectoryIteratorTmpl<NonFilteredIterator>::FilteredDirectoryIteratorTmpl(
    const path &iteratedDir,const string ®exMask
    )
:   FilteredDirectoryIteratorTmpl(iteratedDir, boost::regex(regexMask))
{
}

template <class NonFilteredIterator>
FilteredDirectoryIteratorTmpl<NonFilteredIterator>::FilteredDirectoryIteratorTmpl(
    const path &iteratedDir,const boost::regex ®exMask
    )
:   it(NonFilteredIterator(iteratedDir)),
    filter(
        [regexMask](const boost::filesystem::directory_entry& dirEntry){
            using std::endl;
            // return false to skip dirEntry if no match
            const string filename = dirEntry.path().filename().native();
            return boost::regex_match(filename, regexMask);
        }
        )
{
    if (it!=end && !filter(*it)) ++(*this);
}

template <class NonFilteredIterator>
FilteredDirectoryIteratorTmpl<NonFilteredIterator>::FilteredDirectoryIteratorTmpl(
    const path &iteratedDir, const FilterFunction &filter
    )
:   it(NonFilteredIterator(iteratedDir)),
    filter(filter)
{
    if (it!=end && !filter(*it)) ++(*this);
}

#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文