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
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; }
|