Andy
Andy

Reputation: 151

C++ Searching in all files of a PATH

How should I go about searching the entire files/folders from a given PATH, and searching all of it's contents for a specific string for example?

I know I'd have to do it recursively somehow, unless there's a function to search for everything in a path. And that I'd have to open each file found and check for that specific string in it. But how does Visual Studio open files? Can it open any kind of files, or just text-based ones?

All I've managed to get so far is:

#include <iostream>
#include <filesystem>
using namespace std;
namespace fs = std::experimental::filesystem;

void ShowFiles(string path)
{
    for (auto &p : fs::directory_iterator(path))
        cout << p.path().filename() << '\n';
}

int main()
{
    ShowFiles("D:/TEST/testFOLDER/");
}

Which only prints the folders/files in the given PATH, but not the ones inside folders of it.

EDIT: should i use DIR* and dirent* ? would it be any easier?

Upvotes: 1

Views: 1986

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

Use recursive_directory_iterator instead.

For reading the file, use std::ifstream. It has been updated to take a path object in C++17.

Upvotes: 3

Related Questions