Open all .exe in a folder
Dec 13, 2014 at 9:55pm UTC
I need to make a program that will go through the folder it is in and open all exes that are in it. I can't really find anything on google about it. I'm using windows. Like maybe have something that lists all the files and sees if they have .exe in their name then using ShellExecute to open them.
Dec 14, 2014 at 1:32am UTC
Seems like a job for bash or batch rather than C++. If you insist on C/++, look into popen and CreateProcess.
Dec 14, 2014 at 2:34am UTC
The Microsoft implementation includes the draft TR2 filesystem library.
(With other implementation, use boost::filesystem which provides like functionality.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
#include <vector>
#include <string>
#include <filesystem>
// #include <boost/filesystem.hpp>
std::vector<std::string> filtered_file_list( std::string directory, std::string ends_with = ".exe" , bool recursive = true )
{
namespace fs = std::tr2::sys ;
// namespace fs = boost::filesystem ;
std::vector<std::string> file_list ;
try
{
const auto end = fs::directory_iterator() ;
for ( auto iter = fs::directory_iterator(directory) ; iter != end ; ++iter )
{
const fs::path path = iter->path() ;
if ( recursive && fs::is_directory(path) )
{
for ( std::string str : filtered_file_list( path.string(), ends_with, true ) )
file_list.push_back( std::move(str) ) ;
}
else if ( fs::is_regular_file(path) )
{
std::string str = path.string() ;
const auto n = ends_with.size() ;
if ( str.size() > n && ends_with == str.substr( str.size() - n ) )
file_list.push_back( std::move(str) ) ;
}
}
}
catch ( const std::exception& ) { /* error */ }
return file_list ;
}
int main()
{
// drop privileges, sanitise envirinment, etc. (for ShellExecute)
for ( std::string str : filtered_file_list( "C:\\Program Files" ) )
std::cout << str << '\n' ; // std::system(...) (perhaps through std::async)
}
http://rextester.com/GMNFE49115
Topic archived. No new replies allowed.