How to get list of files with a specific extension in a given folder?

前端 未结 6 1912
孤街浪徒
孤街浪徒 2020-11-27 03:29

I want to get the file names of all files that have a specific extension in a given folder (and recursively, its subfolders). That is, the file name (and extension), not th

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 04:12

    a C++17 code

    #include 
    #include 
    #include 
    namespace fs = std::filesystem;
    
    int main()
    {
        std::string path("/your/dir/");
        std::string ext(".sample");
        for (auto &p : fs::recursive_directory_iterator(path))
        {
            if (p.path().extension() == ext)
                std::cout << p.path().stem().string() << '\n';
        }
        return 0;
    }
    

提交回复
热议问题