动手实现查找小工具:优化版FindEverything

项目背景和意义

查找指定路径下文件名包含某个字段的文件,并打印出来

软件界面设计

部分源代码说明:

【选择】按钮双击事件实现方法,打开文件对话框选择路径:

void CFindEverythingDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	TCHAR           szFolderPath[MAX_PATH] = {0};  
	CString         strFolderPath = TEXT("");  

	BROWSEINFO      sInfo;  
	::ZeroMemory(&sInfo, sizeof(BROWSEINFO));  
	sInfo.pidlRoot   = 0;  
	sInfo.lpszTitle   = _T("请选择待分类图片位置");  
	sInfo.ulFlags   = BIF_RETURNONLYFSDIRS|BIF_EDITBOX|BIF_DONTGOBELOWDOMAIN;  
	sInfo.lpfn     = NULL;  

	// 显示文件夹选择对话框  
	LPITEMIDLIST lpidlBrowse = ::SHBrowseForFolder(&sInfo);   
	if (lpidlBrowse != NULL)  
	{  
		// 取得文件夹名  
		if (::SHGetPathFromIDList(lpidlBrowse,szFolderPath))    
		{  
			strFolderPath = szFolderPath;  
		}  
	}  
	if(lpidlBrowse != NULL)  
	{  
		::CoTaskMemFree(lpidlBrowse);  
	}  

	m_path=strFolderPath;  
	//选择的查找路径显示在文本框IDC_EDIT1中
	GetDlgItem(IDC_EDIT1)->SetWindowTextW(strFolderPath);
}

【查找】按钮实现方法,

void CFindEverythingDlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	MyCountFile m_CountFile;
	vector<string> mvfiles,mvPaths;
	CString findstr;
	GetDlgItem(IDC_EDIT2)->GetWindowTextW(findstr);
	m_CountFile.GetAndPrintFileNames(StringAbout::toString(m_path),mvPaths,StringAbout::toString(findstr));
}

代码中的MyCountFile类的源代码在后面,StringAbout::toString()函数的实现方法参考:string与Cstring字符串类型转换和其他操作总结

查找D:\Program Files (x86)\opencv\build\x86\vc10\lib路径下的d.lib字段,生成filelist文件,fileList.txt文件与FindEverything.exe在同一个文件夹下,如下图所示一共找到38项文件

 

MyCountFile.h头文件

#pragma once
#include <string.h>
#include <vector>
#include <list>
using namespace std;

class MyCountFile
{
public:
	MyCountFile(void);
	~MyCountFile(void);
private:
	vector<string> sourcefiles;
public:
	void GetAndPrintFileNames(string sourcePath,vector<string> &vsrcPath,string findstr);
	void getAllFiles(string path, vector<string>& files,string findstr);
};

MyCountFile.cpp文件

#include "StdAfx.h"
#include "MyCountFile.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include "io.h"

MyCountFile::MyCountFile(void)
{
}


MyCountFile::~MyCountFile(void)
{
}

void MyCountFile::GetAndPrintFileNames(string sourcePath,vector<string> &vsrcPath,string findstr)
{
	vector<string> sourcefiles;
	vector<string> files;
	char* sourcedist = "fileList.txt";
	getAllFiles(sourcePath, sourcefiles,findstr);

	vsrcPath.clear();
	vsrcPath=sourcefiles;

	//将vector中的string保存在txt文件中
	ofstream ofn(sourcedist);
	int size = sourcefiles.size();
	ofn << size << endl;
	for (int i = 0; i < size; i++) {
		ofn << sourcefiles[i] << endl;
	}
	ofn.close();
}

void MyCountFile::getAllFiles(string path, vector<string>& mfiles,string findstr)
{
	//文件句柄
	long hFile = 0;
	//文件信息
	struct _finddata_t fileinfo;  //很少用的文件信息读取结构
	string p;  //string类很有意思的一个赋值函数:assign(),有很多重载版本
	string a = p.assign(path).append("\\*").c_str();
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
		do {
			//比较文件类型是否是文件夹
			if ((fileinfo.attrib & _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
					//files.push_back(p.assign(path).append("\\").append(fileinfo.name));
					getAllFiles(p.assign(path).append("\\").append(fileinfo.name), mfiles,findstr);
				}
			}
			else
			{
				int m = path.find(findstr);
				string name = fileinfo.name;
				int n = name.find(findstr);
				string sub=name.substr(n+1,name.length()-1);
				if(m>0||n>0)
				{
					mfiles.push_back(p.assign(path).append("\\").append(fileinfo.name));

				}

			}

		} while (_findnext(hFile, &fileinfo) == 0);  //寻找下一个,成功返回0,否则-1
		_findclose(hFile);

	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haimianjie2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值