实现一个自动文件重命名器
$ renamer jpeg jpg JPEG jpgHow to do it...
#include <iostream> #include <regex> #include <vector> #include <filesystem> using namespace std; using namespace filesystem;template <typename T> static string replace(string s, const T &replacements) { for (const auto &[pattern, repl] : replacements) { s = regex_replace(s, pattern, repl); } return s; }int main(int argc, char *argv[]) { if (argc < 3 || argc % 2 != 1) { cout << "Usage: " << argv[0] << " <pattern> <replacement> ...\n"; return 1; }vector<pair<regex, string>> patterns; for (int i {1}; i < argc; i += 2) { patterns.emplace_back(argv[i], argv[i + 1]); }for (const auto &entry : recursive_directory_iterator{current_path()}) { path opath {entry.path()}; string rname {replace(opath.filename().string(), patterns)}; path rpath {opath}; rpath.replace_filename(rname);if (opath != rpath) { cout << opath.c_str() << " --> " << rpath.filename().c_str() << '\n'; if (exists(rpath)) { cout << "Error: Can't rename." " Destination file exists.\n"; } else { rename(opath, rpath); } } } }$ ls birthday_party.jpeg holiday_in_dubai.jpgholiday_in_spain.jpg trip_to_new_york.JPEG $ ../renamer jpeg jpg JPEG jpg /Users/tfc/pictures/birthday_party.jpeg --> birthday_party.jpg /Users/tfc/pictures/trip_to_new_york.JPEG --> trip_to_new_york.jpg $ ls birthday_party.jpg holiday_in_dubai.jpg holiday_in_spain.jpg trip_to_new_york.jpg
Last updated