/* 
    WinDirectoryListing.cpp
    Windows version of directory traversal
 */

#include <io.h>

#include <string>
#include <vector>

using namespace std;

vector < string > m_vectData;

void listDir(const char * sdir, int count){
    string str, strDir;
    struct _finddata_t c_file;
    long hFile;

    // Find first file in current directory
    str = sdir + (string) "\\*";
    if( (hFile = _findfirst( str.c_str(), &c_file )) == -1L ){
        fprintf(stderr, "Error opening %s!\n", str.c_str());
        return;
    }

    do{
        //skip if find . and ..
        if ((strcmp(c_file.name, ".") == 0 ||  strcmp(c_file.name, "..") == 0)) {
            continue;
        }

        str = "";
        for (int i=0; i<count; i++)
            str += "  ";//indentation for better viewing

        if (c_file.attrib & _A_SUBDIR){
            strDir = sdir + (string) "\\" + (string) c_file.name;
            str += "D: " + strDir;
            m_vectData.push_back(str);
            listDir(strDir.c_str(), count+1);
            continue;
        }
        //join given path and file name
        str += "F: " + (string) sdir + (string) "\\" + (string) c_file.name;
        m_vectData.push_back(str);
    }while(_findnext( hFile, &c_file ) == 0);
   _findclose( hFile );
   return;
}





int main(int argc, char *argv[]){

    listDir(".", 0);

    for (int i=0; i<m_vectData.size(); i++){
        printf("%s\n", m_vectData[i].c_str());
    }
    printf("Press return to exit\n");
    getchar();

    return 0;
}
