Skip to content

Instantly share code, notes, and snippets.

@loginsmruti
Forked from deysumitkr/getFilesInDir.cpp
Created May 22, 2019 10:07
Show Gist options
  • Save loginsmruti/f6662d98a2b8e17367031b89af1f2df7 to your computer and use it in GitHub Desktop.
Save loginsmruti/f6662d98a2b8e17367031b89af1f2df7 to your computer and use it in GitHub Desktop.

Revisions

  1. @deysumitkr deysumitkr revised this gist Sep 26, 2017. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion getFilesInDir.cpp
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,12 @@
    std::vector<std::string> exts{".jpg", ".png"};
    std::vector<std::string> files = getFilesInDir("/path/to/root/directory/", exts, true);


    // --------------------------------------------------------

    #define BOOST_FILESYSTEM_NO_DEPRECATED
    #include <boost/filesystem.hpp>

    /**
    * @note If extension is empty string ("") then all files are returned
    * @param Path root directory
    @@ -49,4 +53,4 @@ std::vector<std::string> getFilesInDir(const std::string &path, const std::vecto
    outArray.insert(outArray.end(), files.begin(), files.end());
    }
    return outArray;
    }
    }
  2. @deysumitkr deysumitkr revised this gist Sep 26, 2017. No changes.
  3. @deysumitkr deysumitkr renamed this gist Sep 26, 2017. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions getFiles.cpp → getFilesInDir.cpp
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,15 @@
    // usage
    std::vector<std::string> exts{".jpg", ".png"};
    std::vector<std::string> files = getFilesInDir("/path/to/root/directory/", exts, true);

    // --------------------------------------------------------

    /**
    * @note if extension is empty string ("") then all files are returned
    * @param path root directory
    * @param extension string or vector of strings to filter - empty string returns all files - default: ""
    * @param recursive set true to scan directories recursively - default false
    * @return vector of strings with full paths
    * @note If extension is empty string ("") then all files are returned
    * @param Path root directory
    * @param extension String or vector of strings to filter - empty string returns all files - default: ""
    * @param recursive Set true to scan directories recursively - default false
    * @return Vector of strings with full paths
    */

    std::vector<std::string> getFilesInDir(const std::string &path, const std::string &extension="", const bool &recursive=false){
  4. @deysumitkr deysumitkr created this gist Sep 26, 2017.
    46 changes: 46 additions & 0 deletions getFiles.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    /**
    * @note if extension is empty string ("") then all files are returned
    * @param path root directory
    * @param extension string or vector of strings to filter - empty string returns all files - default: ""
    * @param recursive set true to scan directories recursively - default false
    * @return vector of strings with full paths
    */

    std::vector<std::string> getFilesInDir(const std::string &path, const std::string &extension="", const bool &recursive=false){
    std::vector<std::string> files;
    boost::filesystem::path dir(path);
    if(boost::filesystem::exists(path) && boost::filesystem::is_directory(path)){

    if(recursive){
    boost::filesystem::recursive_directory_iterator it(path);
    boost::filesystem::recursive_directory_iterator endit;
    while (it != endit) {
    if(boost::filesystem::is_regular_file(*it) && (extension=="")?true:it->path().extension() == extension) {
    files.push_back(it->path().string());
    }
    ++it;
    }
    }
    else {
    boost::filesystem::directory_iterator it(path);
    boost::filesystem::directory_iterator endit;
    while (it != endit) {
    if(boost::filesystem::is_regular_file(*it) && (extension=="")?true:it->path().extension() == extension) {
    files.push_back(it->path().string());
    }
    ++it;
    }
    }
    }
    return files;
    }

    std::vector<std::string> getFilesInDir(const std::string &path, const std::vector<std::string> &extension, const bool &recursive=false){
    if(extension.size() <=0) return getFilesInDir(path, "", recursive);
    std::vector<std::string> outArray;
    for (const std::string &ext: extension){
    std::vector<std::string> files = getFilesInDir(path, ext, recursive);
    outArray.insert(outArray.end(), files.begin(), files.end());
    }
    return outArray;
    }