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) { HANDLE hFile = INVALID_HANDLE_VALUE; 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) { do { //Skip directories if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes) continue; strFilePath.Format(_T("%s\\%s"), p_strFolderPath, FindFileData.cFileName); p_pFiles->Add(strFilePath); strFilePath.Empty(); } while(FindNextFile(hFile, &FindFileData)); FindClose(hFile); } }
Please give the exact file extension incase if you need to find out only files which has specific extension.