文件搜索

 const int MAX_FILE = 1000;
TCHAR buf[MAX_FILE][MAX_PATH];
int p = 0;

void AddtoBuf(TCHAR* str)
{
 memcpy(buf[p], str, sizeof(TCHAR)* MAX_PATH);
 p++;
}
bool SearchDir(TCHAR* strDir);

void AppendStr(TCHAR* str, TCHAR* strSrc)
{
 int c1 = _tcslen(str);
 int c2 = _tcslen(strSrc);
 memcpy(str+c1, strSrc, c2*sizeof(TCHAR));
}
bool DealDir(TCHAR* strDir, TCHAR* strChildDir)
{
 TCHAR* s = TEXT("//");

 TCHAR tempbuf[MAX_PATH];
 memset(tempbuf, 0, sizeof(TCHAR)*MAX_PATH);
 AppendStr(tempbuf, strDir);
 AppendStr(tempbuf, s);
 AppendStr(tempbuf, strChildDir);
 
 AddtoBuf(tempbuf);

 return SearchDir(tempbuf);
}
bool DealFile(TCHAR* strDir, TCHAR* strFileName)
{
 TCHAR* s = TEXT("//");

 TCHAR tempbuf[MAX_PATH];
 memset(tempbuf, 0, sizeof(TCHAR)*MAX_PATH);
 AppendStr(tempbuf, strDir);
 AppendStr(tempbuf, s);
 AppendStr(tempbuf, strFileName);

 AddtoBuf(tempbuf);
 return false;
}
bool SearchDir(TCHAR* strDir)
{
 TCHAR* s = TEXT("*.*");
 TCHAR* s2 = TEXT("//");

 TCHAR tempbuf[MAX_PATH];
 memset(tempbuf, 0, sizeof(TCHAR)*MAX_PATH);
 AppendStr(tempbuf, strDir);
 AppendStr(tempbuf, s2);
 AppendStr(tempbuf, s);

 WIN32_FIND_DATA hf;
 HANDLE hFile = FindFirstFile(tempbuf, &hf);
 if(INVALID_HANDLE_VALUE==hFile)
 {
  FindClose(hFile);
  return false;
 }


 if (hf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
 {
  DealDir(strDir, hf.cFileName);
 }
 else
 {
  DealFile(strDir, hf.cFileName);
 }

 bool ret = false;
 while (ret=FindNextFile(hFile, &hf))
 {
  if (hf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   DealDir(strDir, hf.cFileName);
   continue;
  }
  else
  {
   DealFile(strDir, hf.cFileName);
   continue;
  }
 }

 FindClose(hFile);
 return false;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值