主要使用两个函数<io.h>中的_findfirst() ,_findnext
使用<io.h>中的_findfirst() ,_findnext 进行文件查找时,句柄类型可能为int64,而不一定是long;
具体的类型有运行环境决定,推荐使用auto类型自动推导合适的类型,再需要时可以用<typeinfo>下的typeid(变量名).name() 得到实际推导出的类型名字;
#pragma once
#include<io.h>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class GetT
{
private:
string path = "*.*";//文件路径。 ps::此为文件名格式,*作为通配符代表所有文件名或文件后缀,可以按需求指定查找确定的文件名或后缀名
_finddata_t fileInfo; //存储找到文件时返回的该文件的信息
public:
void main(string args = "");
void getCurrent(string path = "未输入路径", vector<string> fileDir = {} );//默认参数方便重载调用
};
/// <summary>
/// 主方法
/// </summary>
/// <param name="args"></param>
void GetT::main(string args)
{
getCurrent(path);
}
/// <summary>
/// 通过一个路径(目录)开始查找所有文件(包括文件夹)
/// 按广度优先算法对子文件夹进行同样的查找操作;
/// </summary>
/// <param name="path">要查找的目录</param>
/// <param name="fileDir">查找到结果中其他目录的集合</param>
/// 注意:查找的第一个文件是 “.”,代表当前目录,第二个文件是“..”代表上一级目录;
inline void GetT::getCurrent(string path , vector<string> fileDir)//用来缓存文件夹目录
{
if (path == "未输入路径") path = this->path;
int index = 0;
//TODO::找到第一个文件(即当前目录) ? 继续 :结束
auto handle = _findfirst(path.c_str(), &fileInfo);//_findfirst() 没找到文件时,返回-1,找到了就返回句柄
if (handle == -1) return;
else
{
}
//TODO::找到第二个文件(即上一级目录) ? 继续 :结束
if (0 == _findnext(handle, &fileInfo)) //_findnext() 找到文件时返回0,没找到文件时返回-1
{
}
else return;
//TODO::查找后续的其他文件(即实际显示在资源管理器里的文件或文件夹)
while (1)
{
//没找到下一个文件 ?跳出循环 :继续执行
if (-1 == _findnext64i32(handle, &fileInfo)) break;
//TODO::是文件夹? 记录进fileDir :作为文件处理
if (fileInfo.attrib == _A_SUBDIR)
{
fileDir.push_back(path + "/" + fileInfo.name);
}
else
{
//TODO::此处按需要补充对找到的文件的处理
}
}
//TODO::当前文件夹已经查找完毕,开始查找下一层子文件夹
for (auto sub : fileDir)
{
getCurrent(sub);
}
}
补充一个适合复用的版本:
#pragma once
#include <io.h>
#include <iostream>
#include <atlstr.h>
#include <queue>
using namespace std;
//--------------------------------
extern "C" _declspec(dllexport)
char* GetCurrentDir();
typedef bool (*FileStdCall)(const char* _FilePath);//_对文件的回调函数指针类型
typedef bool (*DirStdCall)(const char* _Directory);//_对文件夹的回调函数指针类型
/// <summary>
/// 遍历文件夹和文件
/// </summary>
/// <param name="_BeginPath">其实文件夹</param>
/// <param name="_DirCall">回调函数,处理查找到的文件夹</param>
/// <param name="_FileCall">回调函数,处理查找到的文件</param>
/// <param name="_ifOnlyFirstDeep"></param>
extern "C" _declspec(dllexport)
void ErgodicDirAndFile(char* _BeginPath = NULL,
DirStdCall _DirCall = NULL,
FileStdCall _FileCall = NULL,
bool _ifOnlyFirstDeep = false)
{
_finddata_t FindData;//_查找文件时存储返回的信息
HANDLE handlet; //_句柄
queue<string> DirQueue;//需要查找的目录队列,char*处理起来太麻烦,担心内存泄露,改用了string。
//参数重载
if (_BeginPath == NULL)
{
_BeginPath = GetCurrentDir();
}
//对目录队列写入第一个元素
DirQueue.push(string(_BeginPath));
//遍历队列所有目标
while (1)
{
string path;
//结束遍历 ? 取出一个元素进行遍历
if (DirQueue.empty())
break;
else {
path = DirQueue.front();
DirQueue.pop();
}
//查找开始
{
//路径检错
if (_access(path.c_str(), 0) == -1)
break;
//查找第一个结果(是上级目录)
auto handle = _findfirst( (path+"\\*.*").c_str(), &FindData);
if (handle == -1) return;
//查找第二个结果(是当前目录)
if (-1 == _findnext(handle, &FindData)) {
cout << "Failed == " << FindData.name << endl;
cout << "Failed == " << path << endl;
return;
}
//查找目录下的其他结果
while(1){
//跳出条件
if (-1 == _findnext(handle, &FindData))
break;
switch (FindData.attrib)
{
//对文件夹
case _A_SUBDIR: {
//子文件夹压入等待队列中
DirQueue.push( path + "\\"+string(FindData.name));
if(_DirCall != NULL)
_DirCall(path.c_str());
break;
}
//对只读文件
case _A_RDONLY: {
}
//对其他文件(正常,隐藏,系统,存档)
case _A_NORMAL:
case _A_HIDDEN:
case _A_SYSTEM:
case _A_ARCH:
{
if(_FileCall != NULL)
_FileCall( (path+"\\"+FindData.name).c_str());
break;
}
default:
break;
}
}
}
//是否只查找一层
if (_ifOnlyFirstDeep == true)
break;
}
}
//------------------------------------
/// <summary>
/// 获取当前工作目录名称
/// 备注::多线程时可能不保证运行安全
/// </summary>
/// <returns></returns>
extern "C" _declspec(dllexport)
char* GetCurrentDir()
{
CString path;
static string result;
{
GetModuleFileName(NULL, path.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
path.ReleaseBuffer();
path = path.Left(path.ReverseFind('\\'));//只取‘\\'左边的部分,(剥离文件名,只留下目录名)
result = string((CW2A)path.GetString());
}
return (char*)result.c_str();
}