I want to write a function that would search for files of a specific format in directories and its sub-directories and create a list of files in a txt file .... but I can’t figure out yet how to do it ... Tell me please.

    2 answers 2

    FindNextFile .

    Not sure what will work, but what if you use this feature? The name of the file found can be pulled out of the WIN32_FIND_DATA structure, something like this: call FindFirstFile, find the file, and then call FindNextFile

     void Copy(LPCTSTR szInDirName, LPCTSTR szOutDirName, bool flag = false) { WIN32_FIND_DATA ffd; HANDLE hFind; TCHAR szFind[MAX_PATH + 1]; TCHAR szInFileName[MAX_PATH + 1]; TCHAR szOutFileName[MAX_PATH + 1]; lstrcpy(szFind, szInDirName); // Ищем файлы с любым именем и расширением lstrcat(szFind, L"\\*.*"); hFind = FindFirstFile(szFind, &ffd); do { // Формируем полный путь (источник) lstrcpy(szInFileName, szInDirName); lstrcat(szInFileName, L"\\"); lstrcat(szInFileName, ffd.cFileName); // Формируем полный путь (результат) lstrcpy(szOutFileName, szOutDirName); lstrcat(szOutFileName, L"\\"); lstrcat(szOutFileName, ffd.cFileName); // Если flag == true, то копируем и папки if(flag) { if(ffd.dwFileAttributes & 0x00000010) { if(lstrcmp(ffd.cFileName, L".") == 0 || lstrcmp(ffd.cFileName, L"..") == 0) continue; CreateDirectory(szOutFileName, NULL); Copy(szInFileName, szOutFileName); } } // Иначе пропускаем папки else if(ffd.dwFileAttributes & 0x00000010) continue; CopyFile(szInFileName, szOutFileName, TRUE); } while(FindNextFile(hFind, &ffd)); FindClose(hFind); } 

    This is an example; it simply searches for files in one directory, subdirectories, copies to the second.

    Of course, this is not as good as the above advice, but it still works.

      There is a library boost :: filesystem . As an example, the program displays all files with the cpp extension in the current directory:

       #include <boost/filesystem.hpp> #include <iostream> namespace fs = boost::filesystem; int main() { for (fs::recursive_directory_iterator it("./"), end; it != end; ++it) { if (it->path().extension() == ".cpp") { std::cout << *it << std::endl; } } return 0; }