前言
使用boost C++ windows遍历文件夹(包含子目录)
参考链接 https://www.cnblogs.com/hellowooorld/p/11382812.html
配合FindFirstFile、FindNextFile搜索文件夹(包含子目录)符合搜索条件的文件
提示:以下是本篇文章正文内容,下面案例可供参考
一、功能代码
#ifndef __GHOS_FILE_HPP__
#define __GHOS_FILE_HPP__
#include<Windows.h>
#include<boost/filesystem.hpp>
#include<boost/format.hpp>
using namespace std;
using namespace boost;
namespace ghos {
namespace file {
namespace boost_file = boost::filesystem;
//enum dirs
//ref url:https://www.cnblogs.com/hellowooorld/p/11382812.html
vector<boost_file::path> enum_dir(boost_file::path dir_path)
{
vector<boost_file::path> ret;
if (!boost_file::is_directory(dir_path))
{
ret.push_back((format("err:\t%s not a valid dir")%dir_path).str());
}
boost_file::recursive_directory_iterator end_iter;
for (boost_file::recursive_directory_iterator iter(dir_path); iter != end_iter; iter++)
{
ret.push_back(iter->path());
}
return ret;
}
//search dir files for defined name
vector<string> search_filenname_Regex(string Regex_path)
{
vector<string> ret;
HANDLE hFile = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA pNextInfo;
hFile = FindFirstFile(Regex_path.c_str(), &pNextInfo);
if (INVALID_HANDLE_VALUE == hFile)
{
ret.push_back((format("warn:\t%s not matched") % Regex_path).str());
return ret;
}
if (pNextInfo.cFileName[0] != '.')//windwos file not begin with . unless . ..
{
ret.push_back(pNextInfo.cFileName);
}
while (FindNextFile(hFile, &pNextInfo))
{
if (pNextInfo.cFileName[0] == '.')
{
continue;
}
ret.push_back(pNextInfo.cFileName);
}
//CloseHandle(hFile); unneed
return ret;
}
//search dir files for defined name find into dirs
vector<string> search_filenname_Regex(string Regex, boost_file::path path)
{
vector<string> ret;
auto enmus = enum_dir(path);
string regex_path = path.string() + Regex;
ret.push_back((format("path:\t%s") % path).str());
auto cur_ret = search_filenname_Regex(regex_path);
ret.insert(ret.end(), cur_ret.begin(), cur_ret.end());
cur_ret.clear();
for (size_t i = 0; i < enmus.size(); i++)
{
if (boost_file::is_directory(enmus[i]))
{
regex_path = enmus[i].string() + "\\" + Regex;
ret.push_back((format("path:\t%s") % enmus[i]).str());
cur_ret = search_filenname_Regex(regex_path);
//ret.resize(ret.size() + cur_ret.size());
ret.insert(ret.end(), cur_ret.begin(), cur_ret.end());
cur_ret.clear();
}
}
return ret;
}
}//namespace file
}//namespace ghos
#endif
二、测试代码
#include"file.hpp"
#include"pe.hpp"
using namespace ghos;
void test_file_search_filenname_Regex();
void test_pe_struct_check();
void test_file_enum_dir();
void test_file_search_filenname_Regex2();
int main()
{
test_file_search_filenname_Regex2();
test_file_search_filenname_Regex();
test_file_enum_dir();
test_pe_struct_check();
return 0;
}
void test_pe_struct_check()
{
auto test_pe_32 = "C:\\OpenSSL-Win32\\aborttest.exe";
auto test_pe_64 = "C:\\OpenSSL-Win64\\aborttest.exe";
auto info1 = pe::struct_check(test_pe_32);
auto info2 = pe::struct_check(test_pe_64);
printf("info1:\t%s\r\ninfo2:\t%s\r\n",info1.c_str(),info2.c_str());
}
void test_file_enum_dir()
{
auto test_dir = "C:\\OpenSSL-Win32\\";
auto ret = file::enum_dir(test_dir);
for (size_t i = 0; i < ret.size(); i++)
{
printf("%ws\t%s\r\n", ret[i].c_str(), file::boost_file::is_directory(ret[i]) ? "dir" : "file");
}
}
void test_file_search_filenname_Regex()
{
auto ret = file::search_filenname_Regex("C:\\OpenSSL-Win32\\*.exe");
for (size_t i = 0; i < ret.size(); i++)
{
printf("%s\r\n", ret[i].c_str());
}
}
void test_file_search_filenname_Regex2()
{
auto ret = file::search_filenname_Regex("*.exe","C:\\OpenSSL-Win32\\");
for (size_t i = 0; i < ret.size(); i++)
{
printf("%s\r\n", ret[i].c_str());
}
}
三、测试结果
console输出结果:
path: "C:\OpenSSL-Win32\"
aborttest.exe
afalgtest.exe
asynciotest.exe
asynctest.exe
bad_dtls_test.exe
bftest.exe
bioprinttest.exe
bio_enc_test.exe
bntest.exe
buildtest_aes.exe
buildtest_asn1.exe
buildtest_asn1t.exe
buildtest_async.exe
buildtest_bio.exe
buildtest_blowfish.exe
buildtest_bn.exe
buildtest_buffer.exe
buildtest_camellia.exe
buildtest_cast.exe
buildtest_cmac.exe
buildtest_cms.exe
buildtest_comp.exe
buildtest_conf.exe
buildtest_conf_api.exe
buildtest_crypto.exe
buildtest_ct.exe
buildtest_des.exe
buildtest_dh.exe
buildtest_dsa.exe
buildtest_dtls1.exe
buildtest_ebcdic.exe
buildtest_ec.exe
buildtest_ecdh.exe
buildtest_ecdsa.exe
buildtest_engine.exe
buildtest_err.exe
buildtest_evp.exe
buildtest_e_os2.exe
buildtest_hmac.exe
buildtest_idea.exe
buildtest_kdf.exe
buildtest_lhash.exe
buildtest_md4.exe
buildtest_md5.exe
buildtest_mdc2.exe
buildtest_modes.exe
buildtest_objects.exe
buildtest_obj_mac.exe
buildtest_ocsp.exe
buildtest_opensslv.exe
buildtest_ossl_typ.exe
buildtest_pem.exe
buildtest_pem2.exe
buildtest_pkcs12.exe
buildtest_pkcs7.exe
buildtest_rand.exe
buildtest_rc2.exe
buildtest_rc4.exe
buildtest_ripemd.exe
buildtest_rsa.exe
buildtest_safestack.exe
buildtest_seed.exe
buildtest_sha.exe
buildtest_srp.exe
buildtest_srtp.exe
buildtest_ssl.exe
buildtest_ssl2.exe
buildtest_stack.exe
buildtest_symhacks.exe
buildtest_tls1.exe
buildtest_ts.exe
buildtest_txt_db.exe
buildtest_ui.exe
buildtest_whrlpool.exe
buildtest_x509.exe
buildtest_x509v3.exe
buildtest_x509_vfy.exe
casttest.exe
cipherlist_test.exe
clienthellotest.exe
constant_time_test.exe
crltest.exe
ct_test.exe
d2i_test.exe
danetest.exe
destest.exe
dhtest.exe
dsatest.exe
dtlstest.exe
dtlsv1listentest.exe
ecdsatest.exe
ectest.exe
enginetest.exe
errtest.exe
evp_extra_test.exe
evp_test.exe
exdatatest.exe
exptest.exe
fatalerrtest.exe
gmdifftest.exe
heartbeat_test.exe
hmactest.exe
ideatest.exe
igetest.exe
md2test.exe
md4test.exe
md5test.exe
mdc2test.exe
memleaktest.exe
ocspapitest.exe
p5_crpt2_test.exe
packettest.exe
pbelutest.exe
randtest.exe
rc2test.exe
rc4test.exe
rc5test.exe
rmdtest.exe
rsa_test.exe
sanitytest.exe
secmemtest.exe
sha1test.exe
sha256t.exe
sha512t.exe
shlibloadtest.exe
srptest.exe
sslapitest.exe
sslcorrupttest.exe
ssltest_old.exe
ssl_test.exe
ssl_test_ctx_test.exe
threadstest.exe
unins000.exe
v3ext.exe
v3nametest.exe
verify_extra_test.exe
versions.exe
wp_test.exe
x509aux.exe
x509_dup_cert_test.exe
x509_time_test.exe
path: "C:\OpenSSL-Win32\bin"
openssl.exe
path: "C:\OpenSSL-Win32\bin\cnf"
warn: C:\OpenSSL-Win32\bin\cnf\*.exe not matched
path: "C:\OpenSSL-Win32\bin\PEM"
warn: C:\OpenSSL-Win32\bin\PEM\*.exe not matched
path: "C:\OpenSSL-Win32\bin\PEM\demoCA"
warn: C:\OpenSSL-Win32\bin\PEM\demoCA\*.exe not matched
path: "C:\OpenSSL-Win32\bin\PEM\demoCA\private"
warn: C:\OpenSSL-Win32\bin\PEM\demoCA\private\*.exe not matched
path: "C:\OpenSSL-Win32\bin\PEM\demoSRP"
warn: C:\OpenSSL-Win32\bin\PEM\demoSRP\*.exe not matched
path: "C:\OpenSSL-Win32\certs"
warn: C:\OpenSSL-Win32\certs\*.exe not matched
path: "C:\OpenSSL-Win32\ct"
warn: C:\OpenSSL-Win32\ct\*.exe not matched
path: "C:\OpenSSL-Win32\d2i-tests"
warn: C:\OpenSSL-Win32\d2i-tests\*.exe not matched
path: "C:\OpenSSL-Win32\exp"
warn: C:\OpenSSL-Win32\exp\*.exe not matched
path: "C:\OpenSSL-Win32\fuzz"
asn1-test.exe
asn1parse-test.exe
bignum-test.exe
bndiv-test.exe
cms-test.exe
conf-test.exe
crl-test.exe
ct-test.exe
server-test.exe
x509-test.exe
path: "C:\OpenSSL-Win32\include"
warn: C:\OpenSSL-Win32\include\*.exe not matched
path: "C:\OpenSSL-Win32\include\openssl"
warn: C:\OpenSSL-Win32\include\openssl\*.exe not matched
path: "C:\OpenSSL-Win32\lib"
warn: C:\OpenSSL-Win32\lib\*.exe not matched
path: "C:\OpenSSL-Win32\lib\MinGW"
warn: C:\OpenSSL-Win32\lib\MinGW\*.exe not matched
path: "C:\OpenSSL-Win32\lib\VC"
warn: C:\OpenSSL-Win32\lib\VC\*.exe not matched
path: "C:\OpenSSL-Win32\lib\VC\static"
warn: C:\OpenSSL-Win32\lib\VC\static\*.exe not matched
path: "C:\OpenSSL-Win32\ocsp-tests"
warn: C:\OpenSSL-Win32\ocsp-tests\*.exe not matched
path: "C:\OpenSSL-Win32\recipes"
warn: C:\OpenSSL-Win32\recipes\*.exe not matched
path: "C:\OpenSSL-Win32\recipes\04-test_pem_data"
warn: C:\OpenSSL-Win32\recipes\04-test_pem_data\*.exe not matched
path: "C:\OpenSSL-Win32\recipes\80-test_ocsp_data"
warn: C:\OpenSSL-Win32\recipes\80-test_ocsp_data\*.exe not matched
path: "C:\OpenSSL-Win32\smime-certs"
warn: C:\OpenSSL-Win32\smime-certs\*.exe not matched
path: "C:\OpenSSL-Win32\ssl-tests"
warn: C:\OpenSSL-Win32\ssl-tests\*.exe not matched
总结
简单的boost C++的一个例子,学习用