API的文件遍历,未使用CFileFind,因为里面牵扯MFC,编个DLL好麻烦。
Posted Matrix_R
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了API的文件遍历,未使用CFileFind,因为里面牵扯MFC,编个DLL好麻烦。相关的知识,希望对你有一定的参考价值。
1 // FindFileDebug.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "FindFileDebug.h" 6 7 #ifdef _DEBUG 8 #define new DEBUG_NEW 9 #endif 10 11 #define IS_DIRECTORY(x) ((x) & (FILE_ATTRIBUTE_DIRECTORY)) 12 #define IS_FAILED (0) 13 14 void TraversFile(CString csPath, CString & csDatabuffer); 15 BOOL IsDot(LPCTSTR ptStr); 16 using namespace std; 17 18 #define TESTPATH ("G:\\Python脚本\\PyCahrm项目") 19 20 int main() 21 { 22 int nRetCode = 0; 23 CString filepath = CString(TESTPATH); 24 CString FileBuffer; 25 TraversFile(filepath, FileBuffer); 26 printf("错误: GetModuleHandle 失败\n"); 27 system("pause"); 28 return nRetCode; 29 } 30 31 32 void TraversFile(CString csPath, CString & csDatabuffer) 33 { 34 CString csPrePath = csPath; 35 CString csNextPath = csPath; 36 CString csNextLine = CString("\r\n"); 37 CString csSlash = CString("\\"); 38 39 WIN32_FIND_DATA FindFileData = { 0 }; 40 HANDLE hFistFind = FindFirstFile(csPath + CString("\\*.*"), (LPWIN32_FIND_DATA)&FindFileData); 41 if ( INVALID_HANDLE_VALUE == hFistFind) 42 { 43 printf("Failed to open file" ); 44 } 45 else 46 { 47 wcout << FindFileData.cFileName; 48 } 49 do { 50 if (IsDot(FindFileData.cFileName)) 51 { 52 continue; 53 } 54 if (IS_DIRECTORY(FindFileData.dwFileAttributes)) 55 { 56 csNextPath += "\\"; 57 csNextPath += FindFileData.cFileName; 58 TraversFile(csNextPath, csDatabuffer); 59 csNextPath = csPrePath; 60 } 61 else 62 { 63 csDatabuffer += csNextPath + csSlash + FindFileData.cFileName + csNextLine; 64 wcout << FindFileData.cFileName; 65 } 66 } while (FindNextFile(hFistFind, (LPWIN32_FIND_DATA)&FindFileData) != IS_FAILED); 67 FindClose(hFistFind); 68 hFistFind = INVALID_HANDLE_VALUE; 69 } 70 71 BOOL IsDot(LPCTSTR ptStr) 72 { 73 size_t ulen = wcslen (ptStr); 74 if (ulen >= 256) 75 { 76 return TRUE; 77 } 78 while (ulen--) 79 { 80 if (ptStr[ulen] != L‘.‘) 81 { 82 return FALSE; 83 } 84 } 85 return TRUE; 86 }
// FindFileDebug.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"#include "FindFileDebug.h"
#ifdef _DEBUG#define new DEBUG_NEW#endif
#define IS_DIRECTORY(x) ((x) & (FILE_ATTRIBUTE_DIRECTORY))#define IS_FAILED (0)
void TraversFile(CString csPath, CString & csDatabuffer);BOOL IsDot(LPCTSTR ptStr);using namespace std;
#define TESTPATH ("G:\\Python脚本\\PyCahrm项目")
int main(){ int nRetCode = 0;CString filepath = CString(TESTPATH);CString FileBuffer;TraversFile(filepath, FileBuffer);printf("错误: GetModuleHandle 失败\n");system("pause"); return nRetCode;}
void TraversFile(CString csPath, CString & csDatabuffer){CString csPrePath = csPath;CString csNextPath = csPath;CString csNextLine = CString("\r\n");CString csSlash = CString("\\");
WIN32_FIND_DATA FindFileData = { 0 };HANDLE hFistFind = FindFirstFile(csPath + CString("\\*.*"), (LPWIN32_FIND_DATA)&FindFileData);if ( INVALID_HANDLE_VALUE == hFistFind){printf("Failed to open file" );}else{wcout << FindFileData.cFileName;}do {if (IsDot(FindFileData.cFileName)){continue;}if (IS_DIRECTORY(FindFileData.dwFileAttributes)){csNextPath += "\\";csNextPath += FindFileData.cFileName;TraversFile(csNextPath, csDatabuffer);csNextPath = csPrePath;}else{csDatabuffer += csNextPath + csSlash + FindFileData.cFileName + csNextLine;wcout << FindFileData.cFileName;}} while (FindNextFile(hFistFind, (LPWIN32_FIND_DATA)&FindFileData) != IS_FAILED);FindClose(hFistFind);hFistFind = INVALID_HANDLE_VALUE;}
BOOL IsDot(LPCTSTR ptStr){size_t ulen = wcslen (ptStr);if (ulen >= 256){return TRUE;}while (ulen--){if (ptStr[ulen] != L‘.‘){return FALSE;}}return TRUE;}
以上是关于API的文件遍历,未使用CFileFind,因为里面牵扯MFC,编个DLL好麻烦。的主要内容,如果未能解决你的问题,请参考以下文章