码迷,mamicode.com
首页 > 编程语言 > 详细

API的文件遍历,未使用CFileFind,因为里面牵扯MFC,编个DLL好麻烦。

时间:2019-01-01 21:45:51      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:程序   data   应用程序   hand   using   python   rect   cstring   ssl   

 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好麻烦。

标签:程序   data   应用程序   hand   using   python   rect   cstring   ssl   

原文地址:https://www.cnblogs.com/matrix-r/p/10206074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!