From 24138055e64fe4e916402770bfc52a37984acac4 Mon Sep 17 00:00:00 2001 From: Zastian Pretorius Date: Sat, 23 Oct 2021 02:05:58 +0200 Subject: [PATCH] added file sorting and make settings in ./config folder --- main.cpp | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index 974674b..bd68ee8 100644 --- a/main.cpp +++ b/main.cpp @@ -7,8 +7,9 @@ std::string musicTypes[] = {".mp3",".wav"}; std::string pictureType[] = {".jpg","jpeg","png"}; namespace fs = std::filesystem; -std::string settingsPath = "settings.conf"; std::string home = getenv("HOME"); +std::string settingsPath = home+"/.config/fsorter/settings.conf"; +std::string sortingPath = fs::current_path(); class typeAndPaths{ public: std::string type; @@ -19,15 +20,19 @@ class typeAndPaths{ void writeSettins() { std::ofstream settings(settingsPath); - settings << "Picture=\n"+ home +"/Pictures\n.jpg,.jpeg,.png" << std::endl; - settings << "Music=\n"+home+"/Music\n.mp3,.wav" << std::endl; - settings << "Video=\n"+home+"/Videos\n.mp4" << std::endl; - settings << "Arcive=\n"+home+"/Documents/Compressed\n.zip,.rar,.7z" << std::endl; + settings << "Picture=\n"+ home +"/Pictures/\n.jpg,.jpeg,.png," << std::endl; + settings << "Music=\n"+home+"/Music/\n.mp3,.wav," << std::endl; + settings << "Video=\n"+home+"/Videos/\n.mp4," << std::endl; + settings << "Arcive=\n"+home+"/Documents/Compressed/\n.zip,.rar,.7z," << std::endl; settings.close(); } std::vector readSettings() { + if(!fs::exists(settingsPath)) + { + fs::create_directory(home + "/.config/fsorter/"); + } std::ifstream settings(settingsPath); std::vector listOfPaths; std::string setting; @@ -98,11 +103,12 @@ void checkSettingsPaths(std::vector Paths) if(tolower(awnser) == 'y') { fs::create_directory(Paths[i].path); + std::cout << "Directorry " << Paths[i].path << " was created\n"; valid = true; } else if (tolower(awnser) == 'n') { - std::cout << "Directory was not created\n"; + std::cout << "Directory" << Paths[i].path <<" was not created\n"; valid = true; } else @@ -114,10 +120,31 @@ void checkSettingsPaths(std::vector Paths) } } +void sortPath(std::string path, std::vector Paths) +{ + for(auto const& file: fs::directory_iterator{path}) + { + if(file.path().has_extension()) + { + std::cout << file.path().extension() << '\n'; + for (int i = 0; i < Paths.size(); i++) { + for (int x = 0; x < Paths[i].extentions.size(); x++) { + if(Paths[i].extentions[x] == file.path().extension()) + { + fs::rename(file.path().string(), Paths[i].path + file.path().filename().string()); + } + } + } + } + + } +} + int main() { std::vector listOfPaths = readSettings(); std::vector Paths = declarePaths(listOfPaths); checkSettingsPaths(Paths); + sortPath(sortingPath, Paths); return 0; }