简单的查找文件

//ReadFolder.h
#include <string>
#include <vector>
using namespace std;

class FileFind{
public:
	FileFind();
	~FileFind();

	FileFind(char* FileName);
	bool TraverseFolder(LPCTSTR lpPath);	//遍历lpPath所指目录
	bool HuntDeskTopFile();	//在桌面上查找目标文件
	void GetTargetFilePath(char* path);	//返回目标文件的绝对路径
	vector<string> GetDriverRoot();		//获取固定磁盘更目录
private:
	char m_FindFileName[MAX_PATH];	//要查找的文件的名称
	char m_FindFilePath[MAX_PATH];	//要查找的文件的绝对路径
};

//ReadFolder.cpp
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <shlwapi.h> 
#include <shlobj.h>
#include <shtypes.h>
#include <shobjidl.h>
#pragma comment(lib,"shlwapi")
#include "ReadFolder.h"

//char FindFileName[128] = "package.exe";	//要查找的文件的名称
//char FindFilePath[MAX_PATH] = {0};	//要查找的文件的绝对路径

FileFind::FileFind()
{
	memset(m_FindFileName,0,sizeof(m_FindFileName));
	memset(m_FindFilePath,0,sizeof(m_FindFilePath));
}
FileFind::~FileFind(){}

FileFind::FileFind(char* FileName)
{
	memset(m_FindFileName,0,sizeof(m_FindFileName));
	memset(m_FindFilePath,0,sizeof(m_FindFilePath));
	strcpy(m_FindFileName,FileName);
}



bool FileFind::TraverseFolder(LPCTSTR lpPath)
{
	TCHAR szFind[MAX_PATH] = {_T("\0")};
	WIN32_FIND_DATA findFileData;
	BOOL bRet;
	bool result = false;

	strcpy(szFind, lpPath);
	strcat(szFind, _T("\\*.*"));     //这里一定要指明通配符,不然不会读取所有文件和目录

	HANDLE hFind = ::FindFirstFile(szFind, &findFileData);
	if (INVALID_HANDLE_VALUE == hFind)
	{
		return false;
	}

	//遍历文件夹
	while (TRUE)
	{
		if (findFileData.cFileName[0] != _T('.'))
		{//不是当前路径或者父目录的快捷方式
			//_tprintf(_T("%s\\%s\n"), lpPath, findFileData.cFileName);
			if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{//这是一个普通目录
				//设置下一个将要扫描的文件夹路径
				strcpy(szFind, lpPath);    
				strcat(szFind, _T("\\"));    
				strcat(szFind, findFileData.cFileName);
				///_tcscat_s(szNextDir, _T("\\*"));
				//遍历该目录
				TraverseFolder(szFind);
			}
			else	//如果不是目录,则进行比较
			{
				//如果文件名相同
				if(strcmp(findFileData.cFileName,m_FindFileName) == 0)
				{
					strcpy(m_FindFilePath,lpPath);
					strcat(m_FindFilePath,_T("\\"));
					strcat(m_FindFilePath,findFileData.cFileName);
					result = true;
					break;
				}
			}
		}
		//如果是当前路径或者父目录的快捷方式,或者是普通目录,则寻找下一个目录或者文件
		bRet = ::FindNextFile(hFind, &findFileData);
		if (!bRet)
		{//函数调用失败
			//cout << "FindNextFile failed, error code: " 
			//  << GetLastError() << endl;
			break;
		}
	}
	FindClose(hFind);

	return result;
}


//遍历桌面文件
bool FileFind::HuntDeskTopFile()
{
	bool result = false;
	HRESULT hr;
	string FileName;
	//vector<string> vec;
	hr=CoInitialize(NULL);
	if (SUCCEEDED(hr))
	{
		IMalloc *pMalloc;
		IShellFolder *pFolder;
		IEnumIDList *pEnumIDList;

		hr=SHGetMalloc(&pMalloc);
		hr=SHGetDesktopFolder(&pFolder);

		LPITEMIDLIST pIDList;

		hr=pFolder->EnumObjects(NULL,SHCONTF_NONFOLDERS,&pEnumIDList);

		STRRET strBuf;
		LPTSTR pszName;
		HRESULT hrEnd;
		do
		{
			hrEnd=pEnumIDList->Next(1,&pIDList,NULL);

			hr=pFolder->GetDisplayNameOf(pIDList,SHGDN_FORPARSING,&strBuf);
			hr=StrRetToStr(&strBuf,pIDList,&pszName);	//此时的pszName是绝对路径
			char szTargetFilePath[MAX_PATH] = {0};
			strcpy(szTargetFilePath,pszName);	//将此时的pszName拷贝到临时变量中
			char* pds = strrchr(pszName,'\\');
			if(pds)
			{
				pds++;
			}else{
				continue;
			}
			if(strcmp(pds,m_FindFileName) == 0)
			{
				strcpy(m_FindFilePath,szTargetFilePath);
				result = true;
				break;
			}
			FileName = pszName;
			//vec.push_back(FileName);
			pMalloc->Free(pszName);
			pMalloc->Free(pIDList);
		}while(hrEnd==S_OK);

		pEnumIDList->Release();
		pFolder->Release();
		pMalloc->Release();
	}
	CoUninitialize();
	return result;
}

//获取根目录
vector<string> FileFind::GetDriverRoot()
{
	CHAR DriverRoot[128] = {0};
	vector<string> Root;
	vector<string> vec;
	GetLogicalDriveStrings(sizeof(DriverRoot),DriverRoot);
	PCHAR OneDriverRoot = DriverRoot;
	do 
	{
		Root.push_back(OneDriverRoot);
		OneDriverRoot += strlen(OneDriverRoot) + 1;
	} while (*OneDriverRoot != '\0');
	for(vector<string>::iterator iter = Root.begin(); iter != Root.end(); iter++)
	{
		int uDriverType = GetDriveType(iter->c_str());
		if(uDriverType == DRIVE_FIXED)
		{
			char *pds = (char*)strrchr(iter->c_str(),'\\');
			if(pds)
			{
				*pds = '\0';
			}
			vec.push_back(iter->c_str());
		}
	}
	return vec;
}

void FileFind::GetTargetFilePath(char* path)
{
	strcpy(path,m_FindFilePath);
}


int main()
{
	vector<string> vec;
	FileFind ff("fct100001.ini");
	char path[MAX_PATH] = {0};
	//先从桌面上查找目标文件,然后再从D盘开始搜索目标文件
	if(!ff.HuntDeskTopFile())
	{
		vec = ff.GetDriverRoot();
		for(vector<string>::iterator iter = vec.begin() + 1; iter != vec.end(); iter++)
		{
			ff.TraverseFolder(iter->c_str());
		}
	}
	ff.GetTargetFilePath(path);
	printf("%s\n",path);
	getchar();
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值