1 #include <Windows.h> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <stack> 6 7 typedef void (__stdcall *P_WALK_DIR_CALLBACK)(const std::string &In_strFilePath); 8 9 int WalkDir(const char *In_pcRoot, P_WALK_DIR_CALLBACK In_pfunCallBack) 10 { 11 int iRetVal = 0; 12 std::string strRoot; 13 std::stack<std::string> stkDirs; 14 15 if (In_pcRoot == NULL || In_pfunCallBack == NULL) 16 { 17 iRetVal = -1; 18 goto fun_ret; 19 } 20 21 strRoot = In_pcRoot; 22 if (strRoot.empty()) 23 { 24 iRetVal = -2; 25 goto fun_ret; 26 } 27 28 if (strRoot.back() != '\\' && strRoot.back() != '/') 29 strRoot += '\\'; 30 stkDirs.push(strRoot); 31 32 while (!stkDirs.empty()) 33 { 34 std::string strDirForWalk; 35 WIN32_FIND_DATAA Win32FindData = {0}; 36 HANDLE hFindHandle = NULL; 37 38 strDirForWalk = stkDirs.top(); 39 stkDirs.pop(); 40 hFindHandle = FindFirstFileA((strDirForWalk + "*").c_str(), &Win32FindData); 41 if (hFindHandle == INVALID_HANDLE_VALUE) 42 continue; 43 44 if (!(strlen(Win32FindData.cFileName) == 1 && strncmp(Win32FindData.cFileName, ".", 1) == 0) 45 && !(strlen(Win32FindData.cFileName) == 2 && strncmp(Win32FindData.cFileName, "..", 2) == 0)) 46 { 47 if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 48 stkDirs.push(strDirForWalk + Win32FindData.cFileName + "\\"); 49 else 50 In_pfunCallBack(strDirForWalk + Win32FindData.cFileName); 51 } 52 while (FindNextFileA(hFindHandle, &Win32FindData)) 53 { 54 if (!(strlen(Win32FindData.cFileName) == 1 && strncmp(Win32FindData.cFileName, ".", 1) == 0) 55 && !(strlen(Win32FindData.cFileName) == 2 && strncmp(Win32FindData.cFileName, "..", 2) == 0)) 56 { 57 if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 58 stkDirs.push(strDirForWalk + Win32FindData.cFileName + "\\"); 59 else 60 In_pfunCallBack(strDirForWalk + Win32FindData.cFileName); 61 } 62 } 63 64 if (hFindHandle != INVALID_HANDLE_VALUE) 65 FindClose(hFindHandle); 66 } 67 68 fun_ret: 69 return iRetVal; 70 } 71 72 void inline __stdcall WalkDirCallBack(const std::string &In_strFilePath) 73 { 74 printf("%s\n", In_strFilePath.c_str()); 75 return; 76 } 77 78 void main(int argc, char **argv) 79 { 80 WalkDir(argv[1], WalkDirCallBack); 81 return; 82 }
本文首发于博客园,任何其他站点均为爬虫或转载,爬虫最无耻。
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 独立开发,这条路可行吗?
· 我在厂里搞 wine 的日子
· 如何通过向量化技术比较两段文本是否相似?
· 35+程序员的转型之路:经济寒冬中的希望与策略
· JavaScript中如何遍历对象?
· C#源生成器:让你的代码飞起来的黑科技
· JavaScript面试题,为什么[] + 0 = '0', 而{} + 0 = 0?
· 简单记录下最近2个月完成的线上系统迁移工作
· 推荐 5 款实用的 Docker 可视化管理工具,工作效率翻倍!
· 好用的浏览器自动化扩展-automa