#include "head.h"
int main(int argc, LPCWSTR argv[])
{
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO));
LPMALLOC pMalloc;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
//定义用户选择的初始路径
TCHAR srcpath[MAX_PATH];
if (pidl != NULL)
{
SHGetPathFromIDList(pidl, srcpath);
printf("你所选择的路径为:%s\n", srcpath);
char *path = srcpath;
if (SUCCEEDED(SHGetMalloc(&pMalloc)))//pidl指向的对象用完应该释放
{
filesearch(path);
pMalloc->Free(pidl);
pMalloc->Release();
}
}
else
{
MessageBox(NULL, TEXT("选择为空"), TEXT("Choose"), MB_OK);
}
//delete srcpath;//记得用完删除
return 0;
}
#include "head.h"
void filesearch(string path)
{
struct _finddata_t filefind;
string curr = path + "\\*.*"; // 修改此处改变搜索条件
int done = 0, handle;
if ((handle = _findfirst(curr.c_str(), &filefind)) != -1)
{
while (!(done = _findnext(handle, &filefind)))
{
if (strcmp(filefind.name, "..") == 0)
continue;
if ((_A_SUBDIR == filefind.attrib)) // 是目录
{
curr = path + "\\" + filefind.name;
filesearch(curr); // 递归遍历子目录
}
else
{
FILE *fp;
char temp[_MAX_PATH];
curr = path + "\\" + filefind.name;
fopen_s(&fp, curr.c_str(), "r"); //std::string类有一个方法叫c_str()就是取出string对象的字符串,实现到char *的转换
if (fp != 0)
{
while (fgets(temp, 255, fp))
{
//temp[strlen(temp) - 1] = '\0';
if (strstr(temp, "windows") != NULL)
{
printf("[File]:%s\n", curr.c_str());
fclose(fp);
}
//else//debug
//{
// //printf("不匹配!\n");
//}
}
}
//else//debug
//{
// //printf("open error!!!\n");
//}
}
}
_findclose(handle);
}
}
#pragma once
#include <Windows.h>
#include <ShlObj.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <io.h>
#include <errno.h>
#include <atlbase.h>
#include <string>
#pragma comment(lib,"User32.lib")
using namespace std;
void filesearch(string path);