zoukankan      html  css  js  c++  java
  • 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 失败
    ");
    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("
    ");
    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 }

    已适配的DLL源文件

      1 // FileFunction20190101Dll.cpp : 定义 DLL 应用程序的导出函数。
      2 //
      3 
      4 #include "stdafx.h"
      5 #include "string"
      6 #define IS_DIRECTORY(x) ((x) & (FILE_ATTRIBUTE_DIRECTORY))
      7 #define IS_FAILED (0)
      8 
      9 using namespace std;
     10 
     11 string g_csBuffer;
     12 
     13 extern "C"
     14 {
     15     BOOL IsDot(LPCSTR ptStr)
     16     {
     17         size_t ulen = strlen(ptStr);
     18         if (ulen >= 256)
     19         {
     20             return TRUE;
     21         }
     22         while (ulen--)
     23         {
     24             if (ptStr[ulen] != '.')
     25             {
     26                 return FALSE;
     27             }
     28         }
     29         return TRUE;
     30     }
     31 }
     32 extern "C"      // 此处extern "c" 为解决c/c++兼容问题 
     33 {
     34     _declspec(dllexport)void TraversFile(string csPath, string & csDatabuffer)
     35     {
     36         string csPrePath = csPath;
     37         string csNextPath = csPath;
     38         string csNextLine = string("
    ");
     39         string csSlash = string("\");
     40 
     41         WIN32_FIND_DATAA FindFileData = { 0 };
     42         HANDLE hFistFind = FindFirstFileA((csPath + string("\*.*")).c_str(), (LPWIN32_FIND_DATAA)&FindFileData);
     43         if (INVALID_HANDLE_VALUE == hFistFind)
     44         {
     45             printf("Failed to open file:%s,%s", csPath.c_str(), string("\*.*").c_str());
     46             return;
     47         }
     48         do {
     49             if (IsDot(FindFileData.cFileName))
     50             {
     51                 continue;
     52             }
     53             if (IS_DIRECTORY(FindFileData.dwFileAttributes))
     54             {
     55                 csNextPath += "\";
     56                 csNextPath += FindFileData.cFileName;
     57                 TraversFile(csNextPath, csDatabuffer);
     58                 csNextPath = csPrePath;
     59             }
     60             else
     61             {
     62                 csDatabuffer += csNextPath + csSlash + FindFileData.cFileName + csNextLine;
     63             }
     64         } while (FindNextFileA(hFistFind, (LPWIN32_FIND_DATAA)&FindFileData) != IS_FAILED);
     65         FindClose(hFistFind);
     66         hFistFind = INVALID_HANDLE_VALUE;
     67     }
     68 }
     69 
     70 
     71 extern "C"
     72 {
     73     _declspec(dllexport)LPCSTR TraversFileInterface(char * pFilePath, int * pLength)
     74     {
     75         if (!g_csBuffer.empty())
     76         {
     77             g_csBuffer.clear();
     78         }
     79         string csPath = string(pFilePath);
     80         TraversFile(csPath, g_csBuffer);
     81         *pLength = g_csBuffer.length();
     82         
     83         return g_csBuffer.c_str();
     84     }
     85 }
     86 
     87 extern "C"
     88 {
     89     _declspec(dllexport)unsigned int TraversFileInterfaceRelease()
     90     {
     91         g_csBuffer.clear();
     92         return 0;
     93     }
     94 }
     95 
     96 extern "C"
     97 {
     98     _declspec(dllexport)unsigned int PrintHex(unsigned char * pData, unsigned int length)
     99     {
    100         for (unsigned int unloop = 0; unloop < length; ++unloop)
    101         {
    102             printf("%02x ", pData[unloop]);
    103         }
    104         return 0;
    105     }
    106 }
  • 相关阅读:
    十大排序算法详解,基本思想+动画演示+C语言实现,太肝了!
    宛如一个未来穿越者,终年33岁的印度数学天才,大数学家哈代说“他发现并创造了数学”
    编程的相关概念
    android中ScrollView嵌套ListView或GridView显示位置问题
    炫酷MD风之dialog各种对话框
    (转载)new Thread的弊端及Java四种线程池的使用
    (转载)Android开发——Android中常见的4种线程池(保证你能看懂并理解)
    (转)android import library switch语句报错case expressions must be constant expressions
    eclipse中将一个项目作为library导入另一个项目中
    (转)Android四大组件——Activity跳转动画、淡出淡入、滑出滑入、自定义退出进入
  • 原文地址:https://www.cnblogs.com/matrix-r/p/10206074.html
Copyright © 2011-2022 走看看