递归遍历文件(文件夹)

/* * author : jmpesp * blog : blog.csdn.net/farcall * function : 递归遍历文件夹 */ #define FOLDERNAME 256 void FindFolder(string szDirPath) { char* FolderName = new char[FOLDERNAME]; ZeroMemory(FolderName,FOLDERNAME); WIN32_FIND_DATA FileData = {0}; HANDLE hSearch = NULL; BOOL fFinished = FALSE; DWORD nLen = NULL; if (!szDirPath.size()) { return; } szDirPath += "//*.*"; hSearch = FindFirstFile(szDirPath.c_str(), &FileData); if (hSearch == INVALID_HANDLE_VALUE) { return; } while (!fFinished) { szDirPath = szDirPath.substr(0,szDirPath.length()-3); szDirPath += FileData.cFileName; if ((FILE_ATTRIBUTE_DIRECTORY & FileData.dwFileAttributes) &&strcmp(FileData.cFileName,".") &&strcmp(FileData.cFileName,"..")) { string szNextDirPath = szDirPath; /* WriteCharToFile(szNextDirPath.data()); printf(szNextDirPath.data()); printf("/r/n"); */ FindFolder(szNextDirPath); DWORD nPos = szDirPath.find_last_of("//"); szDirPath = szDirPath.substr(0,nPos); szDirPath += "//*.*"; } else //非文件夹 { szDirPath = szDirPath.substr(0,szDirPath.length()-strlen(FileData.cFileName)); szDirPath += "*.*"; } if (!FindNextFile(hSearch, &FileData)) { if (GetLastError() == ERROR_NO_MORE_FILES) { fFinished = TRUE; //没有文件 } } } } //仅为测试用 void WriteCharToFile(const char* pChar) { HANDLE hAppend; DWORD dwBytesWritten, dwPos; // Open the existing file, or if the file does not exist, // create a new file. hAppend = CreateFile("c://TWO.TXT", // open TWO.TXT GENERIC_WRITE, // open for writing 0, // do not share NULL, // no security OPEN_ALWAYS, // open or create FILE_ATTRIBUTE_NORMAL, // normal file NULL); // no attr. template if (hAppend == INVALID_HANDLE_VALUE) { return; } dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END); WriteFile(hAppend, pChar, strlen(pChar), &dwBytesWritten, NULL); WriteFile(hAppend, "/r/n", strlen("/r/n"), &dwBytesWritten, NULL); CloseHandle(hAppend); } 

这样的代码本来在网上拷贝一下就可以的 不过弄了半天总是出错

还是自己写个吧,递归实现,查找文件时修改对应的匹配符就OK了

 

记录,备查

你可能感兴趣的:(function,String,File,Blog,Security,null)