Capture Results from System()?

Hello,
I am trying to write a basic program that searches a user's hard drive for PST files. Once the program finds the files, I would like to be able to use that data to copy the files to another location.

So far, my program can find the files I am looking for...but I have not been able to figure out how to capture the information from the system so that I can setup a copy command? Does anyone have any thoughts or suggestions? Thanks in advance!

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

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 
    //PROGRAM TITLE & COLOR
    system("title SearchPST By: maximusava");
    system("color 9B");
    
    
    //VARIABLES
    string findPST = "dir c:\\ /s /b | find \".pst\"";
    string s;
      
    cout << "\nSearching for .PST files...\n\n";
    
    string command = findPST;
    system( command.c_str() ); 
    
/*
 AT THIS POINT I WOULD LIKE TO TAKE THE OUTPUT FROM THE SYSTEM AND 
 USE IT TO WRITE A COMMAND - SO THAT I CAN COPY THE FILES TO A DESIRED LOCATION 
*/    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
You should consider using a .bat file instead of a C++ program. If you install Cygwin, you can use find, xargs, and cp to do the whole job with a single line. (The find.exe that is part of Cygwin is different from the find.exe that is part of DOS and Windows.)

If you want to stay with C++ anyway, change
string findPST = "dir c:\\ /s /b | find \".pst\"";
to
string findPST = "dir c:\\ /s /b | find \".pst\" >temp.tmp";
Then you must read back temp.tmp, copy your files, and delete temp.tmp at the end.
Topic archived. No new replies allowed.