From b3be266a0698cc22008cd3bd164618979e34d659 Mon Sep 17 00:00:00 2001 From: huangyongan Date: Mon, 24 Apr 2023 11:56:00 +0800 Subject: [PATCH] init --- .gitignore | 5 ++ CMakeLists.txt | 6 ++ main.cpp | 162 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8e7a5db --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/build +/cmake-build-* +/.idea +/.vs +/.vscode \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a88d6ea --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(ajx_find_files) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(ajx_find_files main.cpp) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d587fb0 --- /dev/null +++ b/main.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include + +#define K_VISION_DIR "/hard/k_vision" +#define AI_VISION_DIR "/hard/ai_vision" +#define BEGIN_TIME_INTERVAL 8 +#define END_TIME_INTERVAL 12 + +std::vector get_file_names(const std::string& strDir) +{ + std::vector vs; + std::filesystem::path path(strDir); + if (!std::filesystem::exists(path)) + { + return vs; + } + std::filesystem::directory_iterator dir_iter(path); + for (auto it:dir_iter) + { + if (it.is_regular_file() && it.exists()) + { + vs.push_back(it.path().filename().string()); + } + } + return vs; +} + +std::vector get_file_paths(const std::string& strDir) +{ + std::vector vs; + std::filesystem::path path(strDir); + if (!std::filesystem::exists(path)) + { + return vs; + } + std::filesystem::directory_iterator dir_iter(path); + for (auto it:dir_iter) + { + if (it.is_regular_file() && it.exists()) + { + vs.push_back(it.path().string()); + } + } + return vs; +} + +std::vector get_search_paths(const std::string& strFileName, int nBeginTime = 8, int nEndTime = 12) +{ + std::vector vs; + if (strFileName.empty()) + { + return vs; + } + std::string strDate = strFileName.substr(0,13); + int date[6] = {0}; + sscanf(strDate.c_str(),"%02d%02d%02d_%02d%02d%02d",&date[0],&date[1],&date[2],&date[3],&date[4],&date[5]); + struct tm input_time = {0}; + input_time.tm_year = date[0] + 2000 - 1900; + input_time.tm_mon = date[1] - 1; + input_time.tm_mday = date[2]; + input_time.tm_hour = date[3]; + input_time.tm_min = date[4]; + input_time.tm_sec = date[5]; + time_t tt[3] = {0}; + tt[0] = mktime(&input_time); + tt[1] = tt[0] + nBeginTime; + tt[2] = tt[0] + nEndTime; + tm* tm_begin = localtime(&tt[1]); + char szPathBegin[255]; + snprintf(szPathBegin, sizeof(szPathBegin),"/ssd/ok/%d%02d%02d/%d%02d%02d_%02d/%d%02d%02d_%02d%02d/", + tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday, + tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday, tm_begin->tm_hour, + tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday, tm_begin->tm_hour, tm_begin->tm_min); + tm* tm_end = localtime(&tt[2]); + char szPathEnd[255]; + snprintf(szPathEnd, sizeof(szPathEnd),"/ssd/ok/%d%02d%02d/%d%02d%02d_%02d/%d%02d%02d_%02d%02d/", + tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday, + tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday, tm_end->tm_hour, + tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday, tm_end->tm_hour, tm_end->tm_min); + if (std::string(szPathBegin) == std::string(szPathEnd)) + { + vs.push_back(szPathBegin); + } + else + { + vs.push_back(szPathBegin); + vs.push_back(szPathEnd); + } + + return vs; +} + +std::string cvt_filename_to_datetime(const std::string& strFileName) +{ + std::string str; + if (strFileName.empty()) + { + return str; + } + std::string strDate = strFileName.substr(0,13); + int date[6] = {0}; + sscanf(strDate.c_str(),"%02d%02d%02d_%02d%02d%02d",&date[0],&date[1],&date[2],&date[3],&date[4],&date[5]); + char szText[255]; + snprintf(szText, sizeof (szText), "%d%02d%02d_%02d%02d%02d_", + date[0] + 2000, date[1], date[2], date[3], date[4], date[5]); + str = szText; + return str; +} + + +int main() { + auto tt = time(nullptr); + auto tm = localtime(&tt); + char szSaveDir[255]; + snprintf(szSaveDir, sizeof (szSaveDir), "%s/%d%02d%02d/", + AI_VISION_DIR, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); + std::filesystem::create_directories(szSaveDir); + auto vec_file_name = get_file_names(K_VISION_DIR); + if (vec_file_name.empty()) + { + std::cout << "error: K_VISION_DIR is empty" << std::endl; + return -1; + } + std::vector vec_matched_path; + std::cout << "info: >>> match file begin <<<" << std::endl; + for (auto it:vec_file_name) + { + auto datetime = cvt_filename_to_datetime(it); + auto vec_search_path = get_search_paths(it, BEGIN_TIME_INTERVAL, END_TIME_INTERVAL); + for (auto dir:vec_search_path) + { + auto vec_file_path = get_file_paths(dir); + for (auto path:vec_file_path) + { + if (path.find(datetime) != std::string::npos) + { + vec_matched_path.push_back(path); + } + } + } + } + std::cout << "info: >>> match file end(" << vec_matched_path.size() << ") <<<" << std::endl; + std::cout << "info: >>> copy file begin <<<" << std::endl; + int i = 0; + for (auto it:vec_matched_path) + { + std::filesystem::path path(it); + auto strName = path.filename().string(); + std::string strPath = std::string(szSaveDir) + "/" + strName; + bool bRet = std::filesystem::copy_file(it,strName,std::filesystem::copy_options::overwrite_existing); + if (bRet) + { + ++i; + } + } + std::cout << "info: >>> copy file end(" << i << ") <<<" << std::endl; + + return 0; +}