1. 背景
有一批目录下的图片,想改名并放在另外一个文件夹下
2.代码
rename.cpp
#include <iostream>
#include <experimental/filesystem>
#include <iomanip>
namespace fs = std::experimental::filesystem;
std::string src_dir = "/xxx/camera";
std::string dst_dir = "/xxx/c3";
std::string formatNumber(int num, int width)
{
std::stringstream ss;
ss << std::setw(width) << std::setfill('0') << num;
return ss.str();
}
int main() {
// 创建目标目录
fs::create_directory(dst_dir);
// 遍历源目录中的所有文件
for (auto& file : fs::directory_iterator(src_dir))
{
// 获取源文件名和扩展名
std::string src_file = file.path().filename().string();
std::string ext = file.path().extension().string();
std::cout << file << " " << src_file << " " << ext << std::endl;
// 新文件名
//std::string dst_file = src_file.substr(0, src_file.length() - ext.length()) + "_new" + ext;
std::string new_name = src_file.substr(0,src_file.find("."));
std::string dst_file = formatNumber(std::stoi(new_name), 5) + ext;;
// 创建源文件路径和目标文件路径
std::string src_path = src_dir + "/" + src_file;
std::string dst_path = dst_dir + "/" + dst_file;
// 将源文件复制到目标目录中
fs::copy_file(src_path, dst_path);
std::cout << "Copied " << src_path << " to " << dst_path << std::endl;
}
return 0;
}
代码使用了C++17中的文件系统库std::experimental::filesystem
3. CMakeLists.txt
project(rename)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_BUILD_TYPE "Release")
add_compile_options(-W)
add_executable(rename rename.cpp)
target_link_libraries(rename -lstdc++fs)
4.参考
Atlas:undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()
5. 后续
升级版
#include <iostream>
#include <experimental/filesystem>
#include <iomanip>
#include <typeinfo>
#include <cmath>
#include <dlfcn.h>
#include <dirent.h>
#include <algorithm>
namespace fs = std::experimental::filesystem;
std::string base_dir = "/xxx/src";
std::string camera0_dir = base_dir + "/camera_0/";
std::string camera1_dir = base_dir + "/camera_1/";
std::string camera2_dir = base_dir + "/camera_2/";
std::string camera3_dir = base_dir + "/camera_3/";
std::string out_camera_dir = "/xxx/dst";
std::string out_camera_dir0 = out_camera_dir + "/0/";
std::string out_camera_dir1 = out_camera_dir + "/1/";
std::string out_camera_dir2 = out_camera_dir + "/2/";
std::string out_camera_dir3 = out_camera_dir + "/3/";
bool get_filelist_from_dir(std::string _path, std::vector<std::string>& _files);
std::string formatNumber(int num, int width)
{
std::stringstream ss;
ss << std::setw(width) << std::setfill('0') << num;
return ss.str();
}
int min(int a, int b)
{
return a > b ? b : a;
}
int main(){
int num = 1000;
std::vector<std::string> camera0Paths;
camera0Paths.reserve(num);
std::vector<std::string> camera1Paths;
camera1Paths.reserve(num);
std::vector<std::string> camera2Paths;
camera2Paths.reserve(num);
std::vector<std::string> camera3Paths;
camera3Paths.reserve(num);
get_filelist_from_dir(camera0_dir, camera0Paths);
get_filelist_from_dir(camera1_dir, camera1Paths);
get_filelist_from_dir(camera2_dir, camera2Paths);
get_filelist_from_dir(camera3_dir, camera3Paths);
std::vector<std::vector<std::string>> camera_file_Paths;
std::vector<std::string> temp;
temp.reserve(4); //temp.resize(4) std::vector<std::string> temp(4) 都会生成4个对象,如果再push_back n 个,就会vector.size() = n +4
camera_file_Paths.resize(num,temp);
//std::vector<std::vector<std::string>> camera_file_Paths(32, std::vector<std::string>(4));
// std::size_t size = camera0Paths.size();
// if (size != camera1Paths.size() || size != camera2Paths.size() || size != camera3Paths.size()) {
// std::cerr << "Vectors must have equal length\n";
// return 1;
// }
std::sort(camera0Paths.begin(),camera0Paths.end());
std::sort(camera1Paths.begin(),camera1Paths.end());
std::sort(camera2Paths.begin(),camera2Paths.end());
std::sort(camera3Paths.begin(),camera3Paths.end());
std::size_t size = min(camera0Paths.size(), camera1Paths.size()) < min(camera2Paths.size(), camera3Paths.size()) ? min(camera0Paths.size(), camera1Paths.size()) : min(camera2Paths.size(), camera3Paths.size());
std::cout << " size : "<< size << std::endl;
int interval = 0;
int frameCount = 1214;
for (std::size_t i = 0; i < size; ++i) {
std::string elem0 = camera0_dir + camera0Paths[i+interval].c_str();
std::string elem1 = camera1_dir + camera1Paths[i+interval].c_str();
std::string elem2 = camera2_dir + camera2Paths[i+interval].c_str();
std::string elem3 = camera3_dir + camera3Paths[i+interval].c_str();
camera_file_Paths[i].push_back(elem0);
camera_file_Paths[i].push_back(elem1);
camera_file_Paths[i].push_back(elem2);
camera_file_Paths[i].push_back(elem3);
// std::cout << "elem0 " << elem0 << std::endl;
// std::cout << "elem1 " << elem1 << std::endl;
// std::cout << "elem2 " << elem2 << std::endl;
// std::cout << "elem3 " << elem3 << std::endl;
std::string dst_file0 = out_camera_dir0 + formatNumber(frameCount, 5) + ".png";
std::string dst_file1 = out_camera_dir1 + formatNumber(frameCount, 5) + ".png";
std::string dst_file2 = out_camera_dir2 + formatNumber(frameCount, 5) + ".png";
std::string dst_file3 = out_camera_dir3 + formatNumber(frameCount, 5) + ".png";
fs::copy_file(elem0, dst_file0);
fs::copy_file(elem1, dst_file1);
fs::copy_file(elem2, dst_file2);
fs::copy_file(elem3, dst_file3);
std::cout << "Copied " << elem3 << " to " << dst_file3 << std::endl;
frameCount+=1;
}
std::cout << camera_file_Paths.size() << std::endl;
std::cout << camera_file_Paths[0].size() << std::endl;
return 0;
}
bool get_filelist_from_dir(std::string _path, std::vector<std::string>& _files)
{
DIR* dir = opendir(_path.c_str());
struct dirent* ptr;
std::vector<std::string> file;
while((ptr = readdir(dir)) != NULL)
{
if(ptr->d_name[0] == '.') continue;
file.push_back(ptr->d_name);
}
closedir(dir);
sort(file.begin(), file.end());
_files = file;
}