Created
March 18, 2020 13:54
-
-
Save yujp/5e48f4e39551e00cff7df9c304d2a9a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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