Monday, February 15, 2010

C++ Listing all Directories in a Folder.

View the contents of a directory

This is how you get the contents of a directory in Windows with C++.

#include 
#include 

using namespace std;

int main()
{
WIN32_FIND_DATA FindFileData; 
HANDLE hFind = INVALID_HANDLE_VALUE; 
char DirSpec[MAX_PATH]; // directory specification 

cout<<"Path: ";
cin.get(DirSpec, MAX_PATH);
cout<<"\n";
strncat(DirSpec, "\\*", 3);
hFind = FindFirstFile(DirSpec, &FindFileData);

 if(hFind == INVALID_HANDLE_VALUE)
 {
 cout<<"Error: invalid path\n";
 }

cout<<<"\n";

 while(FindNextFile(hFind, &FindFileData) != 0)
 {
 cout<<<"\n";
 }

FindClose(hFind);

return 0;
}