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

时间:2022-11-17 15:59:30
 // 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 = ;
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 = { };
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 >= )
{
return TRUE;
}
while (ulen--)
{
if (ptStr[ulen] != L'.')
{
return FALSE;
}
}
return TRUE;
}

已适配的DLL源文件

 // FileFunction20190101Dll.cpp : 定义 DLL 应用程序的导出函数。
// #include "stdafx.h"
#include "string"
#define IS_DIRECTORY(x) ((x) & (FILE_ATTRIBUTE_DIRECTORY))
#define IS_FAILED (0) using namespace std; string g_csBuffer; extern "C"
{
BOOL IsDot(LPCSTR ptStr)
{
size_t ulen = strlen(ptStr);
if (ulen >= )
{
return TRUE;
}
while (ulen--)
{
if (ptStr[ulen] != '.')
{
return FALSE;
}
}
return TRUE;
}
}
extern "C" // 此处extern "c" 为解决c/c++兼容问题
{
_declspec(dllexport)void TraversFile(string csPath, string & csDatabuffer)
{
string csPrePath = csPath;
string csNextPath = csPath;
string csNextLine = string("\r\n");
string csSlash = string("\\"); WIN32_FIND_DATAA FindFileData = { };
HANDLE hFistFind = FindFirstFileA((csPath + string("\\*.*")).c_str(), (LPWIN32_FIND_DATAA)&FindFileData);
if (INVALID_HANDLE_VALUE == hFistFind)
{
printf("Failed to open file:%s,%s", csPath.c_str(), string("\\*.*").c_str());
return;
}
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;
}
} while (FindNextFileA(hFistFind, (LPWIN32_FIND_DATAA)&FindFileData) != IS_FAILED);
FindClose(hFistFind);
hFistFind = INVALID_HANDLE_VALUE;
}
} extern "C"
{
_declspec(dllexport)LPCSTR TraversFileInterface(char * pFilePath, int * pLength)
{
if (!g_csBuffer.empty())
{
g_csBuffer.clear();
}
string csPath = string(pFilePath);
TraversFile(csPath, g_csBuffer);
*pLength = g_csBuffer.length(); return g_csBuffer.c_str();
}
} extern "C"
{
_declspec(dllexport)unsigned int TraversFileInterfaceRelease()
{
g_csBuffer.clear();
return ;
}
} extern "C"
{
_declspec(dllexport)unsigned int PrintHex(unsigned char * pData, unsigned int length)
{
for (unsigned int unloop = ; unloop < length; ++unloop)
{
printf("%02x ", pData[unloop]);
}
return ;
}
}