First of all the standard C++ library does not provide any such functions to list the files of a directory. You can use boost libraries to get this done. Solution to this problem is not that easy. You will have to implement a lot of techniques to get this done. I can give you all the possible starting pointers.
To port this code to other OS you may need to add OS specific code using # preprocessor directives. Boost Liberay is already cross platform.
First of all you need to get the current path of the directory your program is present in:
For this you can use the following windows os specific code
#include <iostream>
#include <direct.h>
int main(){
char *path = NULL;
path = _getcwd(NULL, 0); // or _getcwd
if (path != NULL)
std::cout << path;
}
Here the path variable contains the address of the current directory. You can pass this path to the next function which will further use it to list the files and directories for you. For listing the directories you can use Boost Liberaries http://www.boost.org/
Next you will need to get all the files and folders present in current directory. Use Boost library for this
Use this sample code from boost
http://www.boost.org/doc/libs/1_37_0/libs/filesystem/example/simple_ls.cpp
Then make a class and store the address received and files names with path in its objects. You can store all the listed address in a object like dir[1].str[size], dir[2].str[size],...
so on.
Now again pass all the folder addresses you received to boost function and get further filenames. All of the above will require many passes.
You can also get the list of files for specific file extension too:
#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
namespace fs = ::boost::filesystem;
// return the filenames of all files that have the specified extension
// in the specified directory and all subdirectories
void get_all(const fs::path& root, const string& ext, vector<fs::path>& ret)
{
if(!fs::exists(root) || !fs::is_directory(root)) return;
fs::recursive_directory_iterator it(root);
fs::recursive_directory_iterator endit;
while(it != endit)
{
if(fs::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename());
++it;
}
}
Finally compare the filenames with the one you want to run and execute it.
The problem can be solved by many other methods too but I think it will be good to start and you can always improve your code.
References:
- How to get list of files with a specific extension in a given folder
- http://www.boost.org/
How do I get a list of files in a directory in C++?
Hope this helps. If you need any further help feel free to ask!