Search partial filenames in C++ using boost filesystem

  boost, boost-filesystem, c++, search

the question is simple , I want to find a file path inside a directory but I have only part of the filename,
so here is a function for this task

bool find_file(const filesystem::path& dir_path, const filesystem::path file_name, filesystem::path& path_found) {
    const fs::recursive_directory_iterator end;
    const auto it = find_if(fs::recursive_directory_iterator(dir_path), end,
                            [file_name](fs::path e) {

         return boost::algorithm::icontains(e.filename().string() ,file_name.string());
    });

    if (it == end) {
        return false;
    } else {
        path_found = it->path();
        return true;
    }
}

unfortunately this function is not working for example if I’m searching for "1200E9408000EC0.jpeg" and the file_name is equal to "000EC0" nothings happen! but if I manually search for this string and change the functions as bellow it finds the file 😐

....
 string search="000EC0";
 return boost::algorithm::icontains(e.filename().string() ,search);
... 

I have no clue whats going on 😐

Source: Windows Questions C++

LEAVE A COMMENT