How to find all files from a folder

29 10 2013

Sometimes we may need to find out all files belongs to a folder. How we will do it?

The FindFirstFile function opens a search handle and returns information about the first file with a name that matches the specified pattern. After the search handle is established, use the FindNextFile function to search for other files that match the same pattern.

void FindFilesFromFolder(CString p_strFolderPath, CStringArray* p_pFiles)
    WIN32_FIND_DATA   FindFileData;
    CString strTemp;

    strTemp.Format(_T("%s\\%s"), p_strFolderPath, _T("\\*.*"));
    hFile = FindFirstFile(strTemp, &FindFileData);
    CString strFilePath;

    if (INVALID_HANDLE_VALUE != hFile) 
            //Skip directories
            if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)

            strFilePath.Format(_T("%s\\%s"), p_strFolderPath, FindFileData.cFileName);
        } while(FindNextFile(hFile, &FindFileData));


Please give the exact file extension incase if you need to find out only files which has specific extension.

How to read filenames from a folder

6 01 2010



How can we read the names of all files in a folder with a specific extension?



Windows has following APIs FindFirstFile and FindNextFile to get the handle of all files in a folder and thus it can extract the relevant information of a file to a WIN32_FIND_DATA structure.



bool FindFilesFromFolder()
   HANDLE            hFile;
   WIN32_FIND_DATA   FindFileData;
   std::vector fileList;

   char chFolderpath[_MAX_PATH];
   CString strExtension   = _T("*.txt");

   strcpy(chFolderpath, _T("D:\\san\\"));
   strcat(chFolderpath, strExtension);

   hFile = FindFirstFile(chFolderpath, &FindFileData);

   if (hFile == INVALID_HANDLE_VALUE) {

      AfxMessageBox(_T("Inavlid file handle."));
      return false;

   CString filepath;


      filepath.Format(_T("%s%s"), _T("D:\\san\\"), FindFileData.cFileName);

   } while(FindNextFile(hFile, &FindFileData));

   return true;



If you want to get all files from a folder irrespective of its extension then just use *.* as filter.