Refactor your code into function and call it recursively when you get directory entry (remember to skip . and ..). Directory can be detected by checking if directory bit is set in data.dwFileAttributes (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY).
Don't do it on C: because you will have to wait a long time. For development create directory C:\Tests and place there few files and folders.
#include <windows.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
// TODO: needs proper error checking, for example when given not existing path.
void GetFilesR( vector<string>& result, const char* path )
{
HANDLE hFind;
WIN32_FIND_DATA data;
string folder( path );
folder += "\\";
string mask( folder );
mask += "*.*";
hFind=FindFirstFile(mask.c_str(),&data);
if(hFind!=INVALID_HANDLE_VALUE)
{
do
{
string name( folder );
name += data.cFileName;
if ( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
// Skip . and .. pseudo folders.
if ( strcmp( data.cFileName, "." ) != 0 && strcmp( data.cFileName, ".." ) != 0 )
{
// TODO: if you want directories appended to result, do it here.
// Add all files from this directory.
GetFilesR( result, name.c_str() );
}
}
else
{
result.push_back( name );
}
} while(FindNextFile(hFind,&data));
}
FindClose(hFind);
}
int main( int argc, char* argv[] )
{
vector<string> files;
// TODO: change directory below.
GetFilesR( files, "C:\\Tests" );
// Print collected files.
for ( vector<string>::iterator i = files.begin() ; i != files.end() ; ++i )
cout << *i << "\n";
return 0;
}
using namespace std, can be removed if you replace all instances off vector width std::vector, string with std::string an cout with std::cout.