Get total number of files in a folder in Windows (C++/MFC)



Code to get total number of files in a folder in Windows (C++/MFC).

 
int GetTotalFiles(CString folderPath)
{
	if (!PathFileExists(folderPath)) return 0;
 
	CFileFind finder;
	CString findString;
	CString filePath;
	int totalFiles = 0;
 
	folderPath.TrimRight(_T('\\'));
	findString = folderPath + _T("\\*.*");
 
	BOOL bWorking = finder.FindFile(findString);
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
 
		if (finder.IsDots())
			continue;
 
		if (finder.IsDirectory()) continue;
 
		totalFiles++;
	}
 
	finder.Close();
 
	return totalFiles;
}


Leave a Reply

Your email address will not be published. Required fields are marked *