#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int ParseDirectory(string dir) {
DIR *tDir;
tDir = opendir(dir.c_str());
if(tDir == nullptr) {
cerr << endl << "Error opening directory " << dir
<< " (errno: " << errno << ")" << endl;
return errno;
}
struct dirent *dirP;
struct stat filestat;
string path;
while( (dirP = readdir(tDir)) ) {
//Skip current object if it is this directory or parent directory
if(!strncmp(dirP->d_name, ".", 1) || !strncmp(dirP->d_name, "..", 2))
continue;
if(dir==".") path = dirP->d_name;
else path = dir + "/" + dirP->d_name;
//Skip current file / directory if it is invalid in some way
if(stat(path.c_str(), &filestat)) continue;
//Recursively call this function if current object is a directory
if(S_ISDIR(filestat.st_mode)) {
ParseDirectory(path);
continue;
}
//At this position you can check if the current file (path) is the file you are searching
}
closedir(tDir);
return 0;
}
i thought it's self-explanatory, well there is not much to say.
It takes the directory dir where you want to start as a parameter (in your case that would be the path of the topmost directory on the disk) and the return value indicates whether there was an error(!=0) or not(==0).
At first it opens the directory dir and then it uses a depth-first search to parse the directory and sub-directories recursively. You'll have to remove line 20-22 if you're using windows and line 24-25 can be simplified to path = dir + "/" + dirP->d_name; in this case.
Maybe you'll need to include other headers in windows aswell, i dont know much about that.