本文最后更新于:2023年4月7日 上午
在网上找了很久如何利用c++批量修改文件名,但是很不幸,找到的都不全,或者跑起来没效果。我就整合了以下批量修改文件名的代码(我跑完之后,文件名并没有改,好奇怪,你们可以试着找一下错误,我感觉没有错啊,为啥改不了。欢迎在评论区解惑)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| int rename_main(){ std::vector<std::string> mesh_dir; std::vector<std::string> keypoint_dir; std::vector<std::string> obj_dir; std::string m = "/Users/wpx/Documents/data/nose_validata/mesh"; std::string key = "/Users/wpx/Documents/data/nose_validata/keypoint"; std::string o = "/Users/wpx/Documents/data/nose_validata/obj"; std::string output_root = "/Users/wpx/Documents/data/nose_validata/output_obj"; mesh_dir = getFilesList(m); keypoint_dir = getFilesList(key); obj_dir = getFilesList(o); for (int k=0; k<mesh_dir.size(); k++){ std::string mesh_name = mesh_dir.at(k); std::string keypoint_name = keypoint_dir.at(k); std::string obj_name = obj_dir.at(k); std::string keypoint_path = "/Users/wpx/Documents/data/nose_validata/keypoint/" + keypoint_name; std::string mesh_path = "/Users/wpx/Documents/data/nose_validata/mesh/" + mesh_name; std::string obj_path = o + "/" + obj_name; int pos_key = keypoint_path.find("_keypoint"); int pos_mesh = mesh_path.find("_mesh"); std::string sub_file_key = keypoint_path.substr(pos_key, 9); std::string sub_file_mesh = mesh_path.substr(pos_mesh, 5); std::string new_key = keypoint_path.replace(pos_key, 9, ""); std::string new_mesh = mesh_path.replace(pos_mesh, 5, ""); std::cout<<new_key<<std::endl; if (!rename(keypoint_path.c_str(), new_key.c_str())) { std::cout << "rename success "<< std::endl; } else { std::cout << "rename error "<< std::endl; }
if (!rename(mesh_path.c_str(), new_mesh.c_str())) { std::cout << "rename success "<< std::endl; } else { std::cout << "rename error "<< std::endl; } } return 0; }
|