Author Topic: VC++ - Enumerating list of files in a folder  (Read 1115 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
VC++ - Enumerating list of files in a folder
« on: December 01, 2011, 07:01:39 pm »
Here is the code to enumerate the list of files in a directory in VC++. It uses FindFirstFile and FindNextFile functions along with WIN32_FIND_DATA struct.

Code:
//Source Code Listing

// searchkey can be = "c:\\kathir\\*.txt";
int GetFileList(const char *searchkey, std::vector<std::string> &list)
{
    WIN32_FIND_DATA fd;
    HANDLE h = FindFirstFile(searchkey,&fd);

    if(h == INVALID_HANDLE_VALUE)
    {
        return 0; // no files found
    }

    while(1)
    {
        list.push_back(fd.cFileName);
        if(FindNextFile(h, &fd) == FALSE)
            break;
    }
    return list.size();
}

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1