当前位置: 首页 > article >正文

静态库封装之ComDir类

ComDir.h


/*
@author:EricsT
@data:20241031
@version:V1.0
@history:
	@author @data @version @content
	EricsT 20241031 V1.0 新增ComDir类[判断存在性以及创建目录]
	EricsT 20241101 V1.1 修改isDirExists函数,新增getFilesFromDir函数
*/

#pragma once

#include <string>
#include <fstream>
#include <vector>
using namespace std;

class ComDir
{
public:

	/*
	func:判断文件夹是否存在
	@strDirPath:文件夹路径
	@csDirPath:文件夹路径
	@pchDirPath:文件夹路径
	ret:存在true,不存在false
	*/
	static bool isDirExists(string strDirPath);
	static bool isDirExists(CString csDirPath);
	static bool isDirExists(PCHAR pchDirPath);

	/*
	func:创建文件夹
	@strDirPath:文件夹路径
	@csDirPath:文件夹路径
	@pchDirPath:文件夹路径
	ret:创建成功true,创建失败false
	*/
	static bool creatDir(string strDirPath);
	static bool creatDir(CString csDirPath);
	static bool creatDir(PCHAR pchDirPath);

	/*
	func:获取文件夹[包括子文件夹]内的文件
	@strDirPath/csDirPath/pchDirPath:文件夹路径
	@strExtension/csExtension/pchExtension:扩展名
	ret:vector<string>文件容器
	*/
	static vector<string> getFilesFromDir(string strDirPath, string strExtension = "*");
	static vector<string> getFilesFromDir(CString csDirPath, CString csExtension = CString("*"));
	static vector<string> getFilesFromDir(PCHAR pchDirPath, PCHAR pchExtension = "*");

};

ComDir.cpp


/*
@author:EricsT
@data:20241031
@version:V1.1
*/

#include "stdafx.h"
#include "ComDir.h"
#include <io.h>
#include <direct.h>

bool ComDir::isDirExists(string strDirPath)
{
	struct _finddata_t fileInfo;

	if (-1 == _findfirst(strDirPath.c_str(), &fileInfo))//取文件信息,不存在则为-1
		return false;

	if (fileInfo.attrib & _A_SUBDIR)
		return true;

	return false;
}

 bool ComDir::isDirExists(CString csDirPath)
{
	//CStringToString
	int len = csDirPath.GetLength();
	PCHAR pch = new char[len + 1];
	size_t pchSize = wcstombs(pch, csDirPath, len + 1);

	if (pchSize == wstring::npos)
	{
		delete pch;
		return "";
	}

	string strDirPath(pch);

	delete pch;

	struct _finddata_t fileInfo;

	if (-1 == _findfirst(strDirPath.c_str(), &fileInfo))
		return false;

	if (fileInfo.attrib & _A_SUBDIR)
		return true;

	return false;
 }

 bool ComDir::isDirExists(PCHAR pchDirPath)
 {
	 struct _finddata_t fileInfo;

	 if (-1 == _findfirst(pchDirPath, &fileInfo))//取文件信息,不存在则为-1
		 return false;

	 if (fileInfo.attrib & _A_SUBDIR)
		 return true;

	 return false;
 }

bool ComDir::creatDir(string strDirPath)
{
	string strCreate = strDirPath;
	string strCoplete = strDirPath.substr(0, 2);//取系统盘

	strCreate = strCreate.substr(3);//除去系统盘之后的路径

	while (true)
	{
		size_t iPos = strCreate.find('\\');

		strCoplete += '\\' + strCreate.substr(0, iPos);//按层级取目录

		if (-1 == _access(strCoplete.c_str(), 0)) //判断该目录是否存在
		{
			if (0 != _mkdir(strCoplete.c_str()))//不存在就创建目录
				return false;
		}

		if (strCreate.npos == iPos)//最后一级目录
			break;

		strCreate = strCreate.substr(iPos + 1);
	}

	return true;
}

bool ComDir::creatDir(CString csDirPath)
{
	//CStringToString
	int len = csDirPath.GetLength();
	PCHAR pch = new char[len + 1];
	size_t pchSize = wcstombs(pch, csDirPath, len + 1);

	if (pchSize == wstring::npos)
	{
		delete pch;
		return "";
	}

	string strDirPath(pch);

	delete pch;

	string strCreate = strDirPath;
	string strCoplete = strDirPath.substr(0, 2);

	strCreate = strCreate.substr(3);

	while (true)
	{
		size_t iPos = strCreate.find('\\');

		strCoplete += '\\' + strCreate.substr(0, iPos);

		if (-1 == _access(strCoplete.c_str(), 0))
		{
			if (0 != _mkdir(strCoplete.c_str()))
				return false;
		}

		if (strCreate.npos == iPos)
			break;

		strCreate = strCreate.substr(iPos + 1);
	}

	return true;
}

bool ComDir::creatDir(PCHAR pchDirPath)
{
	string strDirPath(pchDirPath);
	string strCreate = strDirPath;
	string strCoplete = strDirPath.substr(0, 2);

	strCreate = strCreate.substr(3);

	while (true)
	{
		size_t iPos = strCreate.find('\\');

		strCoplete += '\\' + strCreate.substr(0, iPos);

		if (-1 == _access(strCoplete.c_str(), 0))
		{
			if (0 != _mkdir(strCoplete.c_str()))
				return false;
		}

		if (strCreate.npos == iPos)
			break;

		strCreate = strCreate.substr(iPos + 1);
	}

	return true;
}

std::vector<std::string> ComDir::getFilesFromDir(string strDirPath, string strExtension /*= "*"*/)
{
	vector<string> strRetVec;

	struct _finddata_t fileInfo;
	long hFile = 0;

	if (-1 == (hFile = _findfirst((strDirPath + "\\*.*").c_str(), &fileInfo)))//判断路径是否存在
		return strRetVec;

	do
	{
		if (fileInfo.attrib & _A_SUBDIR)//判断是否是目录
		{
			if (("." == string(fileInfo.name)) || (".." == string(fileInfo.name)))
				continue;
			
			vector<string> vec = getFilesFromDir(strDirPath + "\\" + fileInfo.name, strExtension);//递归获取子目录文件
			strRetVec.insert(strRetVec.end(), vec.begin(), vec.end());//将子容器合并到母容器内
			continue;
		}

		if ("*" == strExtension)//无指定扩展名
		{
			strRetVec.push_back(fileInfo.name);
			continue;
		}

		string strName = fileInfo.name;
		size_t iPos = strName.find_last_of('.');

		if (strName.npos == iPos)
		{
			if ("" == strExtension)
				strRetVec.push_back(fileInfo.name);//目标文件

			continue;
		}

		if (strExtension != strName.substr(iPos + 1))//和指定扩展名不一致
			continue;

		strRetVec.push_back(fileInfo.name);//目标文件

	} while (0 == _findnext(hFile, &fileInfo));

	return strRetVec;
}

std::vector<std::string> ComDir::getFilesFromDir(CString csDirPath, CString csExtension /*= CString("*")*/)
{
	vector<string> strRetVec;

	//CStringToString
	int len = csDirPath.GetLength();
	PCHAR pch = new char[len + 1];
	size_t pchSize = wcstombs(pch, csDirPath, len + 1);
	
	if (pchSize == wstring::npos)
	{
		delete pch;
		return strRetVec;
	}

	string strDirPath(pch);

	delete pch;

	//CStringToString
	int len_1 = csExtension.GetLength();
	PCHAR pch_1 = new char[len_1 + 1];
	size_t pchSize_1 = wcstombs(pch_1, csExtension, len_1 + 1);

	if (pchSize_1 == wstring::npos)
	{
		delete pch_1;
		return strRetVec;
	}

	string strExtension(pch_1);

	delete pch_1;

	struct _finddata_t fileInfo;
	long hFile = 0;

	if (-1 == (hFile = _findfirst((strDirPath + "\\*.*").c_str(), &fileInfo)))
		return strRetVec;

	do
	{
		if (fileInfo.attrib & _A_SUBDIR)
		{
			if (("." == string(fileInfo.name)) || (".." == string(fileInfo.name)))
				continue;

			CString csPath = csDirPath + CString("\\") + CString(fileInfo.name);
			vector<string> vec = getFilesFromDir(csPath, csExtension);
			strRetVec.insert(strRetVec.end(), vec.begin(), vec.end());
			continue;
		}

		if ("*" == strExtension)
		{
			strRetVec.push_back(fileInfo.name);
			continue;
		}

		string strName = fileInfo.name;
		size_t iPos = strName.find_last_of('.');

		if (strName.npos == iPos)
		{
			if ("" == strExtension)
				strRetVec.push_back(fileInfo.name);

			continue;
		}

		if (strExtension != strName.substr(iPos + 1))
			continue;

		strRetVec.push_back(fileInfo.name);

	} while (0 == _findnext(hFile, &fileInfo));

	return strRetVec;
}

std::vector<std::string> ComDir::getFilesFromDir(PCHAR pchDirPath, PCHAR pchExtension /*= "*"*/)
{
	vector<string> strRetVec;

	struct _finddata_t fileInfo;
	long hFile = 0;

	if (-1 == (hFile = _findfirst((string(pchDirPath) + "\\*.*").c_str(), &fileInfo)))
		return strRetVec;

	do
	{
		if (fileInfo.attrib & _A_SUBDIR)
		{
			if (("." == string(fileInfo.name)) || (".." == string(fileInfo.name)))
				continue;

			string strPath = string(pchDirPath) + "\\" + string(fileInfo.name);
			vector<string> vec = getFilesFromDir(strPath.c_str(), pchExtension);
			strRetVec.insert(strRetVec.end(), vec.begin(), vec.end());
			continue;
		}

		if ("*" == string(pchExtension))
		{
			strRetVec.push_back(fileInfo.name);
			continue;
		}

		string strName = fileInfo.name;
		size_t iPos = strName.find_last_of('.');

		if (strName.npos == iPos)
		{
			if ("" == string(pchExtension))
				strRetVec.push_back(fileInfo.name);

			continue;
		}

		if (string(pchExtension) != strName.substr(iPos + 1))
			continue;

		strRetVec.push_back(fileInfo.name);

	} while (0 == _findnext(hFile, &fileInfo));

	return strRetVec;
}

关注灵活就业新业态,关注公账号:贤才宝(贤才宝https://www.51xcbw.com) 


http://www.kler.cn/a/464965.html

相关文章:

  • 机器学习周报-ModernTCN文献阅读
  • 五类推理(逻辑推理、概率推理、图推理、基于深度学习的推理)的开源库 (一)
  • 【LLM-Agent】Building effective agents和典型workflows
  • 【JVM】总结篇-运行时内存篇
  • AF3 AtomAttentionEncoder类解读
  • 通义千问API KEY操作指南
  • 数据仓库建设方案和经验总结
  • 【Hackthebox 中英 Write-Up】Web Request | 分析 HTTP 请求和响应
  • C++高级编程技巧:模板元编程与性能优化实践
  • php Yii2 execl表格导出样式定义
  • 【ArcGISPro/GeoScenePro】解决常见的空间参考和投影问题
  • 并发编程系列(三) -synchronized关键字介绍
  • docker 部署nginx
  • 掌握 Dockerfile:格式、解析器指令、环境变量替换
  • uwsgi中指定了uid为nginx,通过subprocess调用conda时候仍尝试读取/root/.config/conda/.condarc
  • Tailwind CSS 实战:响应式布局最佳实践
  • Python从0到100(八十一):神经网络-Fashion MNIST数据集取得最高的识别准确率
  • CSS 学习之 padding 与图形绘制
  • 实训四 :磁盘管理
  • .net core 依赖注入 Dependency Injection, DI
  • Spring Certified Professional 2024 (2V0-72.22)
  • C#二维数组详解
  • 【项目】智能BI洞察引擎 测试报告
  • Adobe ColdFusion 关键安全漏洞紧急修复
  • 解决virtualbox克隆ubuntu虚拟机之后IP重复的问题
  • 第十七周:Fast R-CNN论文阅读