0

I have folder: C:\Users\Bob\Desktop\SomeFolder.

In the folder "SomeFolder" we 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 say I want to display (list) all files which name start with "abc".

It should look something like this:

std::string path = "path_to_directory"; //C:\Users\Bob\Desktop\SomeFolder
for (auto & p : fs::directory_iterator(path))
std::cout << p << std::endl;

But I need some kind of "filter".

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • Use regular expression library: std::regex http://en.cppreference.com/w/cpp/regex – Asesh Sep 10 '17 at 16:17
  • https://stackoverflow.com/questions/32050444/find-file-starting-with-a-certain-string – Hariom Singh Sep 10 '17 at 16:48
  • possible duplicate https://stackoverflow.com/questions/12774207/fastest-way-to-check-if-a-file-exist-using-standard-c-c11-c – Hariom Singh Sep 10 '17 at 16:50
  • I found that alredy, but that code doesn't work in my case: string directorys = directory + "\\abc*"; std::string strSearch = directorys; WIN32_FIND_DATAA ffd; HANDLE hFind = FindFirstFileA(strSearch.c_str(), &ffd); do { std::string strFile = ffd.cFileName; } while (FindNextFileA(hFind, &ffd) != 0); – Enej Unterajter Sep 10 '17 at 16:52

2 Answers2

3

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;
    }
}

Demo Here


Can use std::regex like following for some "complicated" pattern within the path

std::regex fileMatcher( tempPath + "/abc.*", 
                       // All files that begins with `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;
}

Demo Here

Might have to tweak for Windows Path. I don't have a latest compiler to check on windows

P0W
  • 46,614
  • 9
  • 72
  • 119
  • "No instance of overloaded function std::regex match". Same as Josephnicholas – Enej Unterajter Sep 10 '17 at 17:01
  • @Galik Thanks updated. Didn't see that was available as well. – P0W Sep 10 '17 at 17:07
  • @EnejUnterajter Updated , without `std::regex`. And std::regex will work as well see the demo link – P0W Sep 10 '17 at 17:07
  • @EnejUnterajter You need to include it `#include ` – Galik Sep 10 '17 at 17:08
  • regex already included... no suitable user-defined conversion from std::experimental::filesystem::v1::path to std::string exist? – Enej Unterajter Sep 10 '17 at 17:19
  • @EnejUnterajter Could you check refer the attach link and fix your code ? I gave a running demo as well, what else you want from us ? Write entire application for you ? These are normal error message, you should be able to fix those on your own. – P0W Sep 10 '17 at 17:23
  • will try to resolve it, thank you for your effort :) – Enej Unterajter Sep 10 '17 at 17:26
  • Thank you all for your time and effort. Greetings. If anyone will need this: std::string path = your_directory; for (auto& p : fs::directory_iterator(your_directory)) { std::string file_name = p.path().filename().string(); if (file_name.find("abc") == 0) { std::cout << file_name << std::endl; } } – Enej Unterajter Sep 10 '17 at 17:34
-4

Edited code:

    for(auto & p : fs::directory_iterator(path)){
        if (regex_match(p , regex("(abc)(.*)"))){
            cout <<p<< endl;
        }
    }
    return 0;

it prints the file name starting with the string "abc"