Last active
December 21, 2019 19:56
-
-
Save zant/75995d5b1141d85ce097a2e4fbf2fcf4 to your computer and use it in GitHub Desktop.
Organize files from exercises in folders by chapter
This file contains hidden or 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
| // script to organize files with the form '1.0' to 'chapter1/1.0' | |
| #include <stdio.h> | |
| #include <iostream> | |
| #include <filesystem> | |
| #include <regex> | |
| namespace fs = std::filesystem; | |
| int main () { | |
| std::string path = "."; | |
| for (const auto &entry: fs::directory_iterator(path)) { | |
| // rename and convert to a string | |
| std::string old_name = entry.path(); | |
| // erase the first char of the path | |
| old_name.erase(0,2); | |
| // set new path | |
| std::string new_name = "chapter"; | |
| // match files with a digit | |
| std::regex r("\\d"); | |
| std::smatch m; | |
| std::regex_search(old_name, m, r); | |
| // use dat digit to move files | |
| for (auto c: m) { | |
| // append c to new name | |
| new_name += c; | |
| if (fs::is_directory(old_name)) | |
| continue; | |
| if (!fs::exists(new_name)) | |
| fs::create_directory(new_name); | |
| new_name += "/"; | |
| new_name += old_name; | |
| std::cout << "Moving \"" | |
| << old_name | |
| << "\" " | |
| << "to \"" | |
| << new_name | |
| << "\"" | |
| << std::endl; | |
| //rename | |
| if (std::rename(old_name.c_str(), new_name.c_str())) { | |
| std::perror("Error ranaiming the file"); | |
| return 1; | |
| } | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
re O(n^3)