Skip to content

Instantly share code, notes, and snippets.

@Andersama
Created November 29, 2022 21:13
Show Gist options
  • Select an option

  • Save Andersama/675015388bc7d138b8152b82a63ae76f to your computer and use it in GitHub Desktop.

Select an option

Save Andersama/675015388bc7d138b8152b82a63ae76f to your computer and use it in GitHub Desktop.
// file_organizer.cpp : Defines the entry point for the application.
//
#include "file_organizer.h"
#include <filesystem>
#include <string>
#include <string_view>
#include <memory>
#include <memory_resource>
#include "fmt/format.h"
#include "fmt/printf.h"
int main(int argc, char** argv)
{
if (argc < 3) {
std::cout << "usage: <target_folder_to_organize> <destination_folder> [organization method]\n";
return 0;
}
std::string_view target_folder = argv[1];
std::string_view destination_folder = argv[2];
const std::filesystem::path target_path = target_folder;
const std::filesystem::path destination_path = destination_folder;
bool should_quit = false;
if (!std::filesystem::exists(target_path)) {
std::cout << target_folder << " doesn't exist!\n";
should_quit = true;
} else if (!std::filesystem::is_directory(target_path)) {
std::cout << target_folder << " is not a directory!\n";
should_quit = true;
}
if (!std::filesystem::exists(destination_path)) {
std::cout << target_folder << " doesn't exist!\n";
should_quit = true;
} else if (!std::filesystem::is_directory(destination_path)) {
std::cout << destination_path << " is not a directory!\n";
should_quit = true;
}
if (should_quit) {
return 1;
}
if (std::filesystem::equivalent(target_path, destination_path)) {
std::cout << "error: " << target_path << " is same directory as " << destination_path << "\n";
should_quit = true;
}
if (should_quit) {
return 1;
}
// destination folder should not be a child of target folder!
std::filesystem::path check_path = destination_path;
while (check_path.has_parent_path() && !std::filesystem::equivalent(check_path, check_path.parent_path())) {
if (std::filesystem::equivalent(check_path, target_path)) {
std::cout << "error: " << destination_path << " is part of " << target_path << '\n';
return 1;
}
check_path = check_path.parent_path();
}
// final check
if (std::filesystem::equivalent(check_path, target_path)) {
std::cout << "error: " << destination_path << " is part of " << target_path << '\n';
return 1;
}
// everything's ok as far as we can tell, organize files
/*
std::string extension_str;
extension_str.reserve(32);
std::string target_folder_str;
target_folder_str.reserve(32);
*/
std::string target_file_str;
target_file_str.reserve(512);
for (const auto& dir_entry : std::filesystem::recursive_directory_iterator{ target_path }) {
// only copy over regular files
if (std::filesystem::is_regular_file(dir_entry)) {
// get the last time a file was written to
std::filesystem::file_time_type last_write = std::filesystem::last_write_time(dir_entry);
// change from the "filesystem" clock to the "system" clock
std::chrono::system_clock::time_point system_last_write = std::chrono::clock_cast<std::chrono::system_clock>(last_write);
// transform the timestamp into something we can more easily work with for formatting
time_t time_data = std::chrono::system_clock::to_time_t(system_last_write);
/*
struct tm
{
int tm_sec; // seconds after the minute - [0, 60] including leap second
int tm_min; // minutes after the hour - [0, 59]
int tm_hour; // hours since midnight - [0, 23]
int tm_mday; // day of the month - [1, 31]
int tm_mon; // months since January - [0, 11]
int tm_year; // years since 1900
int tm_wday; // days since Sunday - [0, 6]
int tm_yday; // days since January 1 - [0, 365]
int tm_isdst; // daylight savings time flag
};
*/
// tm utc_tm = *gmtime(&time_data); // if we wanted to use global time
tm local_tm = *localtime(&time_data); // get the "local" time data
const std::filesystem::path& dir_entry_path = dir_entry.path();
//target_folder_str.clear();
//fmt::format_to(std::back_inserter(target_folder_str), "year_{}", (local_tm.tm_year+1900));
//extension_str.clear();
// this allocates, try to avoid this in the future
std::string ext = dir_entry_path.extension().string();
std::string_view ext_no_dot = std::string_view{ ext.data(), ext.size() };
ext_no_dot = ext_no_dot.substr((size_t)(ext_no_dot.size() && ext_no_dot[0] == '.'));
//std::string filename = dir_entry_path.filename();
//fmt::format_to(std::back_inserter(extension_str), "_{}", ext_no_dot);
//std::filesystem::path extension_path = extension_str;
//std::filesystem::path output_path = destination_path / target_folder_str / extension_str;
target_file_str.clear();
fmt::format_to(std::back_inserter(target_file_str), "year_{}/_{}/{}", (local_tm.tm_year + 1900), ext_no_dot,
dir_entry_path.filename().string());
std::filesystem::path output_path = destination_path / target_file_str;
std::filesystem::path output_parent_dir = output_path.parent_path();
std::filesystem::create_directories(output_parent_dir);
std::cout << "copy: [from] " << dir_entry << "\t[to] " << output_path << '\n';
// be careful
std::filesystem::copy(dir_entry, output_path, std::filesystem::copy_options::skip_existing);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment