Skip to content

Instantly share code, notes, and snippets.

@yujp
Created March 18, 2020 13:54
Show Gist options
  • Save yujp/5e48f4e39551e00cff7df9c304d2a9a7 to your computer and use it in GitHub Desktop.
Save yujp/5e48f4e39551e00cff7df9c304d2a9a7 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <filesystem>
#include <regex>
int main(int argc,char *argv[]){
if (argc < 4) {
std::cout << "Usage: \"" << argv[0] << "\" <dir> <pattern> <template>" << std::endl;
return 0;
}
auto &dir = argv[1];
std::regex re(argv[2]);
std::string format = argv[3];
for (auto& di: std::filesystem::directory_iterator(dir)) {
auto &path = di.path();
auto &spath = path.string();
auto &replaced = std::regex_replace(spath, re, format);
if (spath.compare(replaced) == 0) {
// not matched
continue;
}
std::cout << "move: " << spath << ' -> ' << replaced << std::endl;
auto &dpath = std::filesystem::path(replaced);
auto &dir = dpath.parent_path();
std::filesystem::create_directories(dir);
std::filesystem::rename(path, replaced);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment