Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

command prompt results

Status
Not open for further replies.

Neoyanderson

Programmer
Apr 18, 2004
16
US
i was wondering if there was a way to take the results of a command prompt and use it in the code to determine the output. what i want to do is portray if a certain command was successful or there was an error trying to execute it? can this be done?
 
The simplest approach: use int system(const char* cmd) library function.
Code:
int rc = system("dir mydir > mytemp.txt");
if (rc == -1)
{
// system() failed.
}
else
{
// rc value == the value that is returned by the command interpreter.
// Suppose it's OK. Now mytemp.txt file contains dir result
  ifstream res("mytemp.txt");
  // Process the contents...
  res.close();
  remove("mytemp.txt"); // clean up...
}
Of course, it's a code sceleton only. You may form system()'s arg dynamically. You may invoke not only system cmds (define full path to exe). But if you want to know success/fail condition you must know started cmd return codes values. Be careful with the current working directory (where cmd output catched).
More controllabe solution: use CreateProcess() Win API function (or at least _exec() VC++ function family). It's the other story, try system before...
Good luck!


 
You should be knowing this that the output of the command can be redirected to a file in Windows Cmd.exe... With the file, you can do whatever you want to do with simple File programming provided with any language.

C:\> dir > directory.txt

directory.txt will have all the lists....


Cheers,

Ravi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top