如何搜索具有特定patern的文件的路径

时间:2023-01-15 10:18:49

I am looking for a way to find files in a directory ( and its subdirectories) if the files matches to a pattern.

我正在寻找一种方法来查找目录(及其子目录)中的文件,如果文件匹配模式。

I have this code:

我有这个代码:

 inline static void ScanForFiles(std::vector<string> &files,const path &inputPath,string filter="*.*",bool recursive=false)
 {
        typedef vector<boost::filesystem::path> vec;             // store paths,
        vec v;                                // so we can sort them later

        copy(directory_iterator(inputPath), directory_iterator(), back_inserter(v));
        for(int i=0; i<v.size(); i++)
        {
            if(IsDirectory(v[i]))
            {
                if(recursive)
                {
                    ScanForDirs(files,v[i],recursive);
                }
            }
            else
            {
                if(File::IsFile(v[i]))
                {
                    files.push_back(v[i].string());
                }
            }
        }
}

This is working, but it doesn't match pattenrs. For example I want to call this function like this:

这是有效的,但它与pattenrs不匹配。例如,我想这样调用这个函数:

std::vector<string> files;
ScanForFiles(files,"c:\\myImages","*.jpg",true);

and I get the list of all jpeg images in myimages and all of its subfolders.

我得到了myimages及其所有子文件夹中所有jpeg图像的列表。

The current code returns all images and there is no pattern match.

当前代码返回所有图像,并且没有模式匹配。

How can I change the above code to do so?

如何更改上面的代码呢?

1 个解决方案

#1


0  

I've come up with the following snippet:

我想出了以下代码段:

#include <iostream>
#include <string>
#include <boost/regex.hpp>

std::string escapeRegex(const std::string &str) {
  boost::regex esc("([\\^\\.\\$\\|\\(\\)\\[\\]\\*\\+\\?\\/\\\\])");                                                         
  std::string rep("\\\\\\1");
  return regex_replace(str, esc, rep, boost::match_default | boost::format_sed);
}

std::string wildcardToRegex(const std::string &pattern) {
  boost::regex esc("\\\\([\\*\\?])");
  std::string rep(".\\1");
  return regex_replace(escapeRegex(pattern), esc, rep, boost::match_default | boost::format_sed);
}

using namespace std;
using namespace boost;
int main(int argc, char **argv) {
  string pattern = "test/of regexes/*.jpg";
  cout << wildcardToRegex(pattern) << endl;
}

It is heavily based on this question. I hope this helps.

它主要基于这个问题。我希望这有帮助。

#1


0  

I've come up with the following snippet:

我想出了以下代码段:

#include <iostream>
#include <string>
#include <boost/regex.hpp>

std::string escapeRegex(const std::string &str) {
  boost::regex esc("([\\^\\.\\$\\|\\(\\)\\[\\]\\*\\+\\?\\/\\\\])");                                                         
  std::string rep("\\\\\\1");
  return regex_replace(str, esc, rep, boost::match_default | boost::format_sed);
}

std::string wildcardToRegex(const std::string &pattern) {
  boost::regex esc("\\\\([\\*\\?])");
  std::string rep(".\\1");
  return regex_replace(escapeRegex(pattern), esc, rep, boost::match_default | boost::format_sed);
}

using namespace std;
using namespace boost;
int main(int argc, char **argv) {
  string pattern = "test/of regexes/*.jpg";
  cout << wildcardToRegex(pattern) << endl;
}

It is heavily based on this question. I hope this helps.

它主要基于这个问题。我希望这有帮助。