Search code examples
c++boost-filesystem

The best way to ignore files with other extensions when using the C++ experimental <filesystem>?


With the future C++, is there a better way to ignore files with other than wanted extensions than the one shown in the code snippet below?

I am learning the C++ experimental <filesystem> (http://en.cppreference.com/w/cpp/experimental/fs) while writing a simple program that transforms text files from one directory to text file in another directory. The program takes input and output directories via command-line arguments. Only the files with certain extensions (like .csv, .txt, ...) should be processed. The output files should have the .xxx extension.

#include <filesystem>
namespace fs = std::tr2::sys; // the implementation from Visual Studio 2015

    ...
    fs::path srcpath{ argv[1] };
    fs::path destpath{ argv[2] };
    ... 
    for (auto name : fs::directory_iterator(srcpath))
    {
        if (!fs::is_regular_file(name))
            continue;                  // ignore the non-files

        fs::path fnameIn{ name };      // input file name

        // Ignore unwanted extensions (here lowered because of Windows).
        string ext{ lower(fnameIn.extension().string()) };
        if (ext != ".txt" && ext != ".csv")
            continue;

        // Build the output filename path.
        fs::path fnameOut{ destpath / fnameIn.filename().replace_extension(".xxx") };

        ... processing ...
    }

Solution

  • Basically, your question boils down to, "given a string, how do I determine if it matches one of a number of possibilities?" That's pretty trivial: put the possibilities in a std::set:

    //Before loop
    std::set<std::string> wanted_exts = {".txt", ".csv"};
    
    //In loop
    string ext{ lower(fnameIn.extension().string()) };
    if (wanted_exts.find(ext) == wanted_exts.end())
        continue;
    

    You can of course keep wanted_exts around for as long as you like, since it probably won't change. Also, if you have Boost.Containers, I would suggest making wanted_exts a flat_set. That will help minimize allocations.