改用boost的filesystem

master
LZY 3 years ago
parent eb54dc5875
commit 383090c89c

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.5)
project(ajx_find_files) project(ajx_find_files)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
add_executable(ajx_find_files main.cpp) add_executable(ajx_find_files main.cpp)
target_link_libraries(ajx_find_files boost_system boost_filesystem)

@ -1,24 +1,25 @@
#include <iostream> #include <iostream>
#include <filesystem> #include <boost/filesystem.hpp>
#include <vector> #include <vector>
#include <string> #include <string>
#define K_VISION_DIR "/hard/k_vision" #define K_VISION_DIR "/hard/k_vision"
#define AI_VISION_DIR "/hard/ai_vision" #define AI_VISION_DIR "/hard/ai_vision"
namespace fs = boost::filesystem;
std::vector<std::string> get_file_names(const std::string& strDir) std::vector<std::string> get_file_names(const std::string& strDir)
{ {
std::vector<std::string> vs; std::vector<std::string> vs;
std::filesystem::path path(strDir); fs::path path(strDir);
if (!std::filesystem::exists(path)) if (!fs::exists(path))
{ {
return vs; return vs;
} }
std::filesystem::directory_iterator dir_iter(path); fs::directory_iterator dir_iter(path);
for (auto it:dir_iter) for (auto it:dir_iter)
{ {
if (it.is_regular_file() && it.exists()) if (fs::is_regular_file(it) && fs::exists(it))
{ {
vs.push_back(it.path().filename().string()); vs.push_back(it.path().filename().string());
} }
@ -29,15 +30,15 @@ std::vector<std::string> get_file_names(const std::string& strDir)
std::vector<std::string> get_file_paths(const std::string& strDir) std::vector<std::string> get_file_paths(const std::string& strDir)
{ {
std::vector<std::string> vs; std::vector<std::string> vs;
std::filesystem::path path(strDir); fs::path path(strDir);
if (!std::filesystem::exists(path)) if (!fs::exists(path))
{ {
return vs; return vs;
} }
std::filesystem::directory_iterator dir_iter(path); fs::directory_iterator dir_iter(path);
for (auto it:dir_iter) for (auto it:dir_iter)
{ {
if (it.is_regular_file() && it.exists()) if (fs::is_regular_file(it) && fs::exists(it))
{ {
vs.push_back(it.path().string()); vs.push_back(it.path().string());
} }
@ -129,7 +130,7 @@ int main(int argc, char* argv[]) {
char szSaveDir[255]; char szSaveDir[255];
snprintf(szSaveDir, sizeof (szSaveDir), "%s/%d%02d%02d/", snprintf(szSaveDir, sizeof (szSaveDir), "%s/%d%02d%02d/",
AI_VISION_DIR, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); AI_VISION_DIR, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
std::filesystem::create_directories(szSaveDir); fs::create_directories(szSaveDir);
auto vec_file_name = get_file_names(K_VISION_DIR); auto vec_file_name = get_file_names(K_VISION_DIR);
if (vec_file_name.empty()) if (vec_file_name.empty())
{ {
@ -162,14 +163,11 @@ int main(int argc, char* argv[]) {
int i = 0; int i = 0;
for (auto it:vec_matched_path) for (auto it:vec_matched_path)
{ {
std::filesystem::path path(it); fs::path path(it);
auto strName = path.filename().string(); auto strName = path.filename().string();
std::string strPath = std::string(szSaveDir) + "/" + strName; std::string strPath = std::string(szSaveDir) + "/" + strName;
bool bRet = std::filesystem::copy_file(it,strName,std::filesystem::copy_options::overwrite_existing); fs::copy_file(it,strName,fs::copy_option::overwrite_if_exists);
if (bRet) ++i;
{
++i;
}
if (i % 50 == 0) if (i % 50 == 0)
{ {
std::cout << "info: copy progress " << i << "/" << vec_matched_path.size() << std::endl; std::cout << "info: copy progress " << i << "/" << vec_matched_path.size() << std::endl;

Loading…
Cancel
Save