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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| namespace fs = std::filesystem;
int main(int argc, char const *argv[]) { std::string parameters[argc]; std::string LogFilePath = fs::temp_directory_path().append("log.txt").generic_string(); std::ofstream Logs(LogFilePath); int OpenLog = 0; std::string BaseName = ""; std::string TargetExt = ""; fs::path OutDir = fs::current_path(); int FileCount = -1;
auto OutHelpInfo = []() { std::cout << "批量重命名文件" << std::endl; std::cout << "参数说明:" << std::endl; std::cout << " -h, --Help 显示帮助信息" << std::endl; std::cout << " -b, --Base 设置重命名的基准文件名" << std::endl; std::cout << " -l, --Log 开启日志保存重命名记录" << std::endl; std::cout << " -c, --FileCount 设置重命名文件的个数" << std::endl; std::cout << " -o, --OutDir 设置重命名文件的输出目录" << std::endl; std::cout << " -e, --Ext 指定需要重命名文件的后缀名(含分隔符\".\")" << std::endl; };
auto UpperStr = [](std::string str) { std::string result; for (auto &c : str) result += toupper(c); return result; }; if (argc < 3) { OutHelpInfo(); return -1; } for (int i = 0; i < argc; i++) { parameters[i] = argv[i]; if (parameters[i] == "-h" || parameters[i] == "--help") { OutHelpInfo(); return 0; } else if (parameters[i] == "-b" || parameters[i] == "--Base") { if (i + 1 < argc) BaseName = argv[i + 1]; else { std::cout << "错误: 参数 -b 缺少参数" << std::endl; OutHelpInfo(); return -1; } } else if (parameters[i] == "-l" || parameters[i] == "--Log") OpenLog = 1; else if (parameters[i] == "-c" || parameters[i] == "--FileCount") { if (i + 1 < argc) FileCount = std::stoi(argv[i + 1]); else { std::cout << "错误: 参数 -c 缺少参数" << std::endl; OutHelpInfo(); return -1; } } else if (parameters[i] == "-o" || parameters[i] == "--OutDir") { if (i + 1 < argc) OutDir = fs::absolute(argv[i + 1]); else { std::cout << "错误: 参数 -o 缺少参数" << std::endl; OutHelpInfo(); return -1; } } else if (parameters[i] == "-e" || parameters[i] == "--Ext") { if (i + 1 < argc) TargetExt = UpperStr(std::string(argv[i + 1])); else { std::cout << "错误: 参数 -e 缺少参数" << std::endl; OutHelpInfo(); return -1; } } }
if (!OpenLog) Logs.close();
std::cout << "基准文件名: " << BaseName << std::endl; if (OpenLog) std::cout << "已开启开启日志" << std::endl; if (FileCount != -1) std::cout << "重命名文件个数: " << FileCount << std::endl; if (TargetExt != "") std::cout << "目标文件后缀: " << TargetExt << std::endl; std::cout << "输出目录: " << OutDir.generic_string() << std::endl; std::cout << "是否开始批量重命名文件?(Y/N): ";
char c; std::cin >> c; if (c != 'Y' && c != 'y') return 0;
fs::path currentPath = fs::current_path(); int count = 1, ID = 0; for (auto &file : fs::directory_iterator(currentPath)) { if (FileCount != -1 && count > FileCount) break; if (file.is_directory()) continue; std::string Extension = UpperStr(file.path().extension().generic_string()); if (!TargetExt.empty() && Extension != TargetExt) continue; fs::path ToName = OutDir / (BaseName + std::to_string(ID++) + Extension); if (fs::exists(ToName)) { if (OpenLog) { std::cout << "文件 \"" << ToName << "\" 已存在, 跳过。" << std::endl; ID = ID + 1; } continue; } if (fs::is_symlink(file)) { if (OpenLog) std::cout << "文件 \"" << file.path() << "\" 是软链接, 跳过。" << std::endl; continue; } if (OpenLog) { Logs << file.path() << " => " << ToName << std::endl; std::cout << "Renaming: " << file.path() << " => " << ToName << std::endl; } fs::rename(file.path(), ToName); count++; } if (OpenLog) { Logs.close(); fs::rename(LogFilePath, currentPath / "log.txt"); std::cout << "重命名完成,日志保存在: " << (currentPath / "log.txt").generic_string() << std::endl; } return 0; }
|