executing the windows command prompt

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 12
Reputation: Sam Lehman is an unknown quantity at this point 
Solved Threads: 0
Sam Lehman Sam Lehman is offline Offline
Newbie Poster

executing the windows command prompt

 
0
  #1
May 15th, 2005
hi,
I need to find a way to get a list of all of the files in the current folder, so i thought i could have my program execute the 'dir' command into the command prompt and then parse what is returened.

would anyone have any ideas on how I could execute the 'dir' command from my program?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 12
Reputation: Sam Lehman is an unknown quantity at this point 
Solved Threads: 0
Sam Lehman Sam Lehman is offline Offline
Newbie Poster

Re: executing the windows command prompt

 
0
  #2
May 15th, 2005
i couldnt find the edit button, so i'll use the quick reply.

i found that system("dir"); works well.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: executing the windows command prompt

 
0
  #3
May 15th, 2005
You can call an external program with the system() function:
  1. #include <stdlib.h>
  2.  
  3. system("DIR");
But that's probably not what you want since you need the output. In that case, you need to create a pipe either through the Win32 API, or with a compiler extension if supported. For example, on Visual C++ .NET you would use SetCurrentDirectory to change the current working directory to the one you want, then _popen to create a pipe to the DIR program. After that it's just a matter of reading the FILE pointer returned by _popen just like any other stream:
  1. #include <stdio.h>
  2. #include <windows.h>
  3.  
  4. int main(void)
  5. {
  6. FILE *fp;
  7.  
  8. SetCurrentDirectory("C:\\");
  9.  
  10. fp = _popen("DIR", "r");
  11.  
  12. if (fp != NULL) {
  13. char buffer[BUFSIZ];
  14.  
  15. while (fgets(buffer, sizeof buffer, fp) != NULL)
  16. fputs(buffer, stdout);
  17. }
  18.  
  19. return 0;
  20. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 12
Reputation: Sam Lehman is an unknown quantity at this point 
Solved Threads: 0
Sam Lehman Sam Lehman is offline Offline
Newbie Poster

Re: executing the windows command prompt

 
0
  #4
May 15th, 2005
thanks, but the following works good enough
  1. void listDatFiles()
  2. {
  3. cout <<endl << "All .dat files in this folder are as followed:" << endl << "-----------------------" <<endl;
  4. system("dir *.dat /B /O N");
  5. displayContinue();
  6.  
  7. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC