项目背景和意义
查找指定路径下文件名包含某个字段的文件,并打印出来
软件界面设计
部分源代码说明:
【选择】按钮双击事件实现方法,打开文件对话框选择路径:
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);
}
}