Skip to content

Instantly share code, notes, and snippets.

@zant
Last active December 21, 2019 19:56
Show Gist options
  • Select an option

  • Save zant/75995d5b1141d85ce097a2e4fbf2fcf4 to your computer and use it in GitHub Desktop.

Select an option

Save zant/75995d5b1141d85ce097a2e4fbf2fcf4 to your computer and use it in GitHub Desktop.
Organize files from exercises in folders by chapter
// 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;
}
}
}
}
@zant
Copy link
Author

zant commented Dec 21, 2019

re O(n^3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment