/*
ressource: http://en.cppreference.com/w/cpp/fileysystem
*/
# include <iostream>
# include <locale>
# include <fstream>
# include <regex>
usingnamespace std;
#include <filesystem>
usingnamespace std::experimental::filesystem;
void scan(path const& folder);
//function to scan the current folder and its subfolders
void rscan(path const& folder);
int main(int argc, char* argv[])
{
//path objects have a value_type of std::basic_string <value_type> where value_type is wchar_t
// either unicaode or asccii values
// 1. create a path object that begins with the current folder
// "." represents the current folder
path current = argv[2];
string switch = "";
switch = argv[1];
cout << "normal scan" << endl;
scan(current);
cout << "recursive scan" << endl;
rscan(current);
//cout << relative_path << endl;
//scan(relative_path);
system("pause");
}
// function to scan the current folder
void scan( string switch , path const& folder)
{
cout << "\n Scanning current folder: \n";
directory_iterator dir(folder); // points to the beginning of the folder
directory_iterator end; // points to the end
while (dir != end)
{
cout << dir->path();
if (is_directory(dir->status()))
{
if (switch == 'c')
{
regex extensions("\\.(cpp|c|h|hpp$");
if (regex_search(dir->path().extension(), extensions))
{
cout << "[dir]";
cout << "ext - " << dir->path().extension() << endl;
cout << "filename = " << dir->path().filename() << endl;
}
}
++dir;
}
cout << endl;
}
void rscan2(path const& folder)
{
cout << "\n Scanning current folder: \n";
recursive_directory_iterator dir(folder); // points to the beginning of the folder
recursive_directory_iterator end; // points to the end
while (dir != end)
{
cout << dir->path();
if (is_directory(dir->status()))
{
cout << "[dir]";
cout << "ext - " << dir->path().extension() << endl;
cout << "filename = " << dir->path().filename() << endl;
}
++dir;
}
cout << endl;
}
void rscan(path const& f)
{
cout << "\nScanning current folder and its subfolders now:\n";
cout << "Recursive scan, Version 1:\n";
//create a recursive directory iterator passing it the folder object
//dir points to the first directory in the folder, the root of the search
recursive_directory_iterator dir(f);
//Create another recursive directory iterator passing it no value
//end points to the end point of the search, end of any folder
recursive_directory_iterator end;
//let's go into the folder
while (dir != end)
{
//Print path, if dir or not, and ext, and filename
cout << dir->path();
if (is_directory(dir->status()))
{
cout << " [dir]";
}
else
{
cout << "";
}
cout << " ext = " << dir->path().extension() << endl;
cout << " filename = " << dir->path().filename() << endl;
++dir;
}
}
What do you mean by "i can't use dir->path.extension() with regex_search on line 84"? If you're getting an error message, what does it say?
In any case, as written, this program contains an invalid regex. regex extensions("\\.(cpp|c|h|hpp$");
gcc/libstdc++ throws regex error saying "Parenthesis is not closed."
clang/libc++ throws regex_error saying "The expression contained mismatched ( and )."
#include <iostream>
#include <string>
#include <vector>
#include <experimental/filesystem>
#include <regex>
#include <type_traits>
namespace fs = std::experimental::filesystem ;
// list of paths of all files under the directory 'dir' when the extenstion matches the regex
// file_list<true> searches recursively into sub-directories; file_list<false> searches only the specified directory
template < bool RECURSIVE > std::vector<fs::path> file_list( fs::path dir, std::regex ext_pattern )
{
std::vector<fs::path> result ;
using iterator = std::conditional< RECURSIVE,
fs::recursive_directory_iterator, fs::directory_iterator >::type ;
const iterator end ;
for( iterator iter { dir } ; iter != end ; ++iter )
{
const std::string extension = iter->path().extension().string() ;
if( fs::is_regular_file(*iter) && std::regex_match( extension, ext_pattern ) ) result.push_back( *iter ) ;
}
return result ;
}
// literal '.' followed by one of "cpp", "cc", "cxx", "h", "hh", "hpp" or "hxx"
// note: ?: indicates that it is a non-capturing group
staticconst std::regex cpp_files( "\\.(?:cpp|cc|cxx|h|hh|hpp|hxx)" ) ;
// non-recursive scan for c++ files: if dir is omitted, current directory is scanned
std::vector<fs::path> scan_cpp_files( fs::path dir = "." ) { return file_list<false>( dir, cpp_files ) ; }
// recursive scan for c++ files: if dir is omitted, current directory is scanned
std::vector<fs::path> rscan_cpp_files( fs::path dir = "." ) { return file_list<true>( dir, cpp_files ) ; }