1. 程式人生 > >c++17 filesystem, regex 遍歷目錄

c++17 filesystem, regex 遍歷目錄

擴展 clas include directory dir 正則 cout 目錄 rect

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去掉擴展名
    }
}

c++17 filesystem, regex 遍歷目錄