CFileFind finder; BOOL bWorking = finder.FindFile(_T("C:\\MyFolder\\*.*")); while (bWorking) { bWorking = finder.FindNextFile(); if (finder.IsDots()) continue; if (finder.IsDirectory()) continue; wprintf_s(L"%s\n", (LPCTSTR) finder.GetFileName()); }
CFileFind finder; BOOL bWorking = finder.FindFile(_T("C:\\MyFolder\\MyFile.txt")); if (!bWorking) { wprintf_s(L"File not found.\n"); } else if (finder.IsDirectory()) { wprintf_s(L"Sorry, this is a directory.\n"); } else { wprintf_s(L"File found: %s\n", (LPCTSTR) finder.GetFileName()); }In this example, we search for a specific file (MyFile.txt) in the C:\MyFolder directory. If the file is found, we print its name by calling the GetFileName method. If it is not found, we print an error message. If a directory with the same name exists, we print a message indicating that it is a directory. In conclusion, CFileFind is a useful class in C++ for searching for files and directories in a directory. It is a part of the MFC library and can be found in the afx.h header file.