Add printing of usage, Add prepending a number for copied files

This commit is contained in:
Eero Holmala 2023-11-06 10:38:20 +02:00
parent 838d0cdd5b
commit ba95940601

View File

@ -1,3 +1,4 @@
#include <string>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <filesystem> #include <filesystem>
@ -5,7 +6,7 @@
void void
print_usage() print_usage()
{ {
std::cout << "\tUsage:\n\t\tflatten <directory_path>\n";
} }
bool bool
@ -17,21 +18,24 @@ CreateDumpDirectory(std::filesystem::path& path) {
} }
void void
RecursivelyCopyDirectoryFiles(const std::filesystem::path& cwd, const std::filesystem::path& dumpDirectory) { RecursivelyCopyDirectoryFiles(const std::filesystem::path& cwd, const std::filesystem::path& dumpDirectory, int& counter) {
for (const auto& entry : std::filesystem::directory_iterator(cwd)) { for (const auto& entry : std::filesystem::directory_iterator(cwd)) {
const auto filenameStr = entry.path().filename().string(); const auto filenameStr = entry.path().filename().string();
if (entry.is_directory()) { if (entry.is_directory()) {
RecursivelyCopyDirectoryFiles(entry.path(), dumpDirectory); std::cout << "Entering " << cwd << "\n";
RecursivelyCopyDirectoryFiles(entry.path(), dumpDirectory, counter);
} }
else if (entry.is_regular_file()) { else if (entry.is_regular_file()) {
auto dest = dumpDirectory; auto dest = dumpDirectory;
dest /= filenameStr; dest /= std::to_string(counter)+"_"+filenameStr;
if(!std::filesystem::exists(dest)){ if(!std::filesystem::exists(dest)){
std::cout << "Copying from " << cwd << " to " << dest << "\n"; std::cout << "Copying from " << cwd << " to " << dest << "\n";
if(!std::filesystem::copy_file(entry.path(), dest)){ if(!std::filesystem::copy_file(entry.path(), dest)){
std::cout << "Could not copy from " << cwd << " to " << dest << "\n"; std::cout << "Could not copy from " << cwd << " to " << dest << "\n";
} else {
counter++;
} }
} }
} }
@ -51,7 +55,8 @@ main(int argc, char** argv){
std::cout << "Could not create directory to flatten to!\n"; std::cout << "Could not create directory to flatten to!\n";
return 1; return 1;
} }
RecursivelyCopyDirectoryFiles(std::filesystem::path(argv[1]), dumpPath); int counter = 0;
RecursivelyCopyDirectoryFiles(std::filesystem::path(argv[1]), dumpPath, counter);
std::cout << "Done" << "\n"; std::cout << "Done" << "\n";
return 0; return 0;
} }