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;
}
在VS編譯器內會報C4996錯誤,解決見下文:
C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. - EricsT - 部落格園 (cnblogs.com)