c++ - List files in directory with same name -
i have folder: c:\users\bob\desktop\somefolder.
in folder "somefolder" have 10 files:
abc.txt, abc1.txt, abc2.txt, abc3.txt, abc4.txt,
xyz.txt, xyz1.txt, xyz2.txt, xyz3.txt, xyz4.txt.
now let's want display (list) files name start "abc".
it should this:
std::string path = "path_to_directory"; //c:\users\bob\desktop\somefolder (auto & p : fs::directory_iterator(path)) std::cout << p << std::endl;
but need kind of "filter".
simply use std::string::find
for(auto& p: fs::directory_iterator(temppath)) { std::string file_name = p.path().filename(); if ( file_name.find("abc") == 0 ) { std::cout << file_name <<std::endl; } }
can use std::regex
following "complicated" pattern within path
std::regex filematcher( temppath + "/abc.*", // files begins `abc` in current directory . std::regex_constants::ecmascript | std::regex_constants::icase); for(auto& p: fs::directory_iterator(temppath)) if (std::regex_match (p.path().c_str() ,filematcher )) { std::cout << p <<std::endl; }
might have tweak windows path. don't have latest compiler check on windows
Comments
Post a Comment