windows - Open an .exe file without knowing the full path in C++ -
i want create program opens .exe
file in c++, want program able run directory, not know full address of file.
eg-
program make present in folder, in turn has subfolders, further contains javaportable.exe
. want program run above mentioned .exe
without getting full path. cd operator in normal dos navigation. also, if possible, want select .exe
, run through javaportable.exe
, , present in previous folder.
first of standard c++ library not provide such functions list files of directory. can use boost libraries done. solution problem not easy. have implement lot of techniques done. can give possible starting pointers.
to port code other os may need add os specific code using # preprocessor directives. boost liberay cross platform.
first of need current path of directory program present in:
for can use 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 path variable contains address of current directory. can pass path next function further use list files , directories you. listing directories can use boost liberaries http://www.boost.org/
next need files , folders present in current directory. use boost library this
use sample code boost http://www.boost.org/doc/libs/1_37_0/libs/filesystem/example/simple_ls.cpp
then make class , store address received , files names path in objects. can store listed address in object dir[1].str[size], dir[2].str[size],...
so on. again pass folder addresses received boost function , further filenames. of above require many passes. can list of files specific file extension too:
#define boost_filesystem_version 3 #define boost_filesystem_no_deprecated #include <boost/filesystem.hpp> namespace fs = ::boost::filesystem; // return filenames of files have specified extension // in specified directory , 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 filenames 1 want run , execute it.
the problem can solved many other methods think start , can improve code.
references:
- how list of files specific extension in given folder
- http://www.boost.org/
how list of files in directory in c++?
hope helps. if need further feel free ask!
Comments
Post a Comment