递归查找文件的Win32代码

#include "Stdafx.h" #include "fileOperation.h" typedef BOOL (CALLBACK *PENUMDIRTREE_CALLBACK)(PCSTR, PVOID); typedef BOOL (CALLBACK *PENUMDIRTREE_CALLBACKW)(PCWSTR, PVOID); static inline BOOL is_sep(char ch) {return ch == '/' || ch == '//';} static inline BOOL is_sepW(WCHAR ch) {return ch == '/' || ch == '//';} BOOL WINAPI _SymMatchFileNameW(PCWSTR file, PCWSTR match, PWSTR* filestop, PWSTR* matchstop) { PCWSTR fptr; PCWSTR mptr; fptr = file + wcslen(file) - 1; mptr = match + wcslen(match) - 1; // It is more efficient that traversing from back to front. while (fptr >= file && mptr >= match) { if (toupper(*fptr) != toupper(*mptr) && !(is_sep(*fptr) && is_sep(*mptr))) break; fptr--; mptr--; } if (filestop) *filestop = (PWSTR)fptr; if (matchstop) *matchstop = (PWSTR)mptr; return mptr == match - 1; } static BOOL _do_searchW(PCWSTR file, PWSTR buffer, BOOL recurse, PENUMDIRTREE_CALLBACKW cb, PVOID user) { HANDLE h; WIN32_FIND_DATAW fd; unsigned pos; BOOL found = FALSE; static const WCHAR S_AllW[] = {'*','.','*','/0'}; static const WCHAR S_DotW[] = {'.','/0'}; static const WCHAR S_DotDotW[] = {'.','.','/0'}; pos = wcslen(buffer); if (buffer[pos - 1] != '//') buffer[pos++] = '//'; wcscpy(buffer + pos, S_AllW); if ((h = FindFirstFileW(buffer, &fd)) == INVALID_HANDLE_VALUE) return FALSE; /* doc doesn't specify how the tree is enumerated... * doing a depth first based on, but may be wrong */ do { if (!wcscmp(fd.cFileName, S_DotW) || !wcscmp(fd.cFileName, S_DotDotW)) continue; wcscpy(buffer + pos, fd.cFileName); if (recurse && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) found = _do_searchW(file, buffer, TRUE, cb, user); else if (_SymMatchFileNameW(buffer, file, NULL, NULL)) { if (!cb || cb(buffer, user)) found = TRUE; } } while (!found && FindNextFileW(h, &fd)); if (!found) buffer[--pos] = '/0'; FindClose(h); return found; } BOOL WINAPI _SearchTreeForFileW(PCWSTR root, PCWSTR file, PWSTR buffer) { wcscpy(buffer, root); return _do_searchW(file, buffer, TRUE, NULL, NULL); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值