63 lines
1.8 KiB
C++
63 lines
1.8 KiB
C++
#include <string>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <filesystem>
|
|
|
|
void
|
|
print_usage()
|
|
{
|
|
std::cout << "\tUsage:\n\t\tflatten <directory_path>\n";
|
|
}
|
|
|
|
bool
|
|
CreateDumpDirectory(std::filesystem::path& path) {
|
|
if(!std::filesystem::exists(path))
|
|
return std::filesystem::create_directory(path);
|
|
else
|
|
return true;
|
|
}
|
|
|
|
void
|
|
RecursivelyCopyDirectoryFiles(const std::filesystem::path& cwd, const std::filesystem::path& dumpDirectory, int& counter) {
|
|
|
|
for (const auto& entry : std::filesystem::directory_iterator(cwd)) {
|
|
const auto filenameStr = entry.path().filename().string();
|
|
|
|
if (entry.is_directory()) {
|
|
std::cout << "Entering " << cwd << "\n";
|
|
RecursivelyCopyDirectoryFiles(entry.path(), dumpDirectory, counter);
|
|
}
|
|
else if (entry.is_regular_file()) {
|
|
auto dest = dumpDirectory;
|
|
dest /= std::to_string(counter)+"_"+filenameStr;
|
|
if(!std::filesystem::exists(dest)){
|
|
std::cout << "Copying from " << cwd << " to " << dest << "\n";
|
|
if(!std::filesystem::copy_file(entry.path(), dest)){
|
|
std::cout << "Could not copy from " << cwd << " to " << dest << "\n";
|
|
} else {
|
|
counter++;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
int
|
|
main(int argc, char** argv){
|
|
std::filesystem::path dumpPath{"dump_directory"};
|
|
if(argc == 1){
|
|
print_usage();
|
|
return 0;
|
|
}
|
|
if(!CreateDumpDirectory(dumpPath)){
|
|
std::cout << "Could not create directory to flatten to!\n";
|
|
return 1;
|
|
}
|
|
int counter = 0;
|
|
RecursivelyCopyDirectoryFiles(std::filesystem::path(argv[1]), dumpPath, counter);
|
|
std::cout << "Done" << "\n";
|
|
return 0;
|
|
}
|