Assuming it is Windows, something like this:
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <cstdlib>
#include <string>
int main()
{
const std::string path = "C:\\some_dir\\another_dir\\" ;
const std::string srce_pattern = "*.txt" ;
const std::string dest_file_name = "merge.txt" ;
std::system( ( "del " + path + dest_file_name ).c_str() ) ;
std::system( ( "copy /a " + path + srce_pattern + " " + path + dest_file_name ).c_str() ) ;
}
|
Note: There is nothing wrong in using
std::system()
or in general one program invoking another program (fork/exec, CreateProcess etc.).
In security concious code, precautions have to be taken:
The environment (PATH etc) has to be sanitized before invoking external programs.
If the data to be passed to external programs (the command string in
std::system()
) come from unreliable sources, it has to be sanitized too.
And privileges have to be dropped to the minimum level required before invocation (do not run as root etc.)
Doing all this is somewhat difficult; so if an alternative is available (for example library calls) it is better to use that rather than invoking an external program to do the job.