1 |
torben |
328 |
// include STL headers |
2 |
|
|
#include <iostream> |
3 |
|
|
#include <string> |
4 |
|
|
#include <vector> |
5 |
|
|
#include <algorithm> |
6 |
|
|
|
7 |
|
|
// c style system-headers |
8 |
|
|
#include <sys/types.h> |
9 |
|
|
#include <dirent.h> |
10 |
|
|
#include <errno.h> |
11 |
|
|
#include <cstring> |
12 |
|
|
|
13 |
|
|
|
14 |
|
|
int main(int argc, char *argv[]) |
15 |
|
|
{ |
16 |
|
|
if (argc < 2) { |
17 |
|
|
std::cout << "run-dir.exe by Torben Nielsen" << std::endl; |
18 |
|
|
std::cout << "runs all (.exe) files in a directory, with optional args" << std::endl; |
19 |
|
|
std::cout << "Usage: run-dir.exe <directory> <command args>" << std::endl; |
20 |
|
|
std::cout << "Example: run-dir.exe c:\\update /passive /norestart" << std::endl; |
21 |
|
|
|
22 |
|
|
return 0; |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
DIR *directory = opendir( argv[1] ); |
26 |
|
|
|
27 |
|
|
if (directory == NULL) { |
28 |
|
|
std::cout << "Could not read directory " << argv[1] << " - "; |
29 |
|
|
std::cout << strerror(errno) << std::endl; |
30 |
|
|
return 1; |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
std::vector<std::string> files; |
34 |
|
|
dirent *file; |
35 |
|
|
while ( (file = readdir(directory)) != NULL ) { |
36 |
|
|
std::string filename = file->d_name; |
37 |
|
|
|
38 |
|
|
if (filename.size() < 5) |
39 |
|
|
continue; |
40 |
|
|
std::string ext = filename.substr(filename.size()-4,4); |
41 |
|
|
|
42 |
|
|
if (ext == ".exe") |
43 |
|
|
files.push_back( filename ); |
44 |
|
|
} |
45 |
|
|
closedir(directory); |
46 |
|
|
|
47 |
|
|
std::string arguments = ""; |
48 |
|
|
|
49 |
|
|
if (argc>2) { |
50 |
|
|
for (int i=2;i<argc;i++) |
51 |
|
|
arguments.append(" ").append(argv[i]); |
52 |
|
|
} |
53 |
|
|
sort(files.begin(), files.end()); |
54 |
|
|
|
55 |
|
|
for (std::vector<std::string>::size_type i = 0; i < files.size(); i++) { |
56 |
|
|
std::string fullname = argv[1]; |
57 |
|
|
fullname.append("\\").append(files[i]); |
58 |
|
|
fullname.append(arguments); |
59 |
|
|
std::cout << "executing " << fullname << std::endl; |
60 |
|
|
int errorval = system(fullname.c_str()); |
61 |
|
|
if (errorval != 0) |
62 |
|
|
std::cerr << "Warning: " << files[i] << " returned " << errorval << std::endl; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
} |