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?

Recommended Answers

All 3 Replies

i couldnt find the edit button, so i'll use the quick reply.

i found that system("dir"); works well.

You can call an external program with the system() function:

#include <stdlib.h>

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:

#include <stdio.h>
#include <windows.h>

int main(void)
{
  FILE *fp;

  SetCurrentDirectory("C:\\");

  fp = _popen("DIR", "r");

  if (fp != NULL) {
    char buffer[BUFSIZ];

    while (fgets(buffer, sizeof buffer, fp) != NULL)
      fputs(buffer, stdout);
  }

  return 0;
}

thanks, but the following works good enough

void listDatFiles()
{
	cout <<endl << "All .dat files in this folder are as followed:" << endl << "-----------------------" <<endl;
	system("dir *.dat /B /O N"); 
	displayContinue();

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.