c++17 filesystem, regex 遍历目录

c++17 FS 还是挺好用的


#include<filesystem>
#include<regex>  //正则表达式

namespace fs = std::experimental::filesystem;


int main()
{
    string strPath = "D:\\pic\\new";
    regex fileSuffix("(.*)(.jpg)");// *.jpg, *.png  

    //regex fileSuffix("(.*).(.jpg)"); 也行
    //regex fileSuffix(".*z.*\\.(jpg|png)");//包含字母z的所有jpg或png图片

    for (auto&DirectoryIter : fs::directory_iterator(strPath))
    {
        auto filepath = DirectoryIter.path();
        auto filename = filepath.filename();
    if (std::regex_match(filename.string(), fileSuffix))
    {
        vecFilePath.push_back(filepath.string());
        cout << filepath << endl;
    }
    //replace_extension替换扩展名
    //stem去掉扩展名
    }
}

猜你喜欢

转载自www.cnblogs.com/scotth/p/9581734.html