From cb7c21fb72045a396564f16ebf0065d4b7be9d86 Mon Sep 17 00:00:00 2001 From: Zastian Pretorius Date: Fri, 22 Oct 2021 19:37:12 +0200 Subject: [PATCH 1/4] made it possobe to read from settings.conf --- .gitignore | 1 + main.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/.gitignore b/.gitignore index fbff541..6d3f1b5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ tmp/ fsorter .ccls-cache/ CMakeFiles/ +settings.conf diff --git a/main.cpp b/main.cpp index cae12df..290bf26 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,72 @@ #include #include #include +#include //#include #include + namespace fs = std::filesystem; +std::string settingsPath = "settings.conf"; +std::string musicPath() +{ + std::string musicFolder = ""; + return musicFolder; +} + +std::string photoPath() +{ + std::string photoFolder = ""; + return photoFolder; +} + +std::string videoPath() +{ + std::string videoFolder = ""; + return videoFolder; +} + +std::string arcivePath() +{ + std::string arciveFolder = ""; + return arciveFolder; +} +void writeSettins() +{ + std::ofstream settings(settingsPath); + settings << "Pictures=\n$HOME/Pictures" << std::endl; + settings << "Music=\n$HOME/Music" << std::endl; + settings.close(); + + +} +std::vector readSettings() +{ + + std::ifstream settings(settingsPath); + std::vector listOfPaths; + std::string setting; + // creates a default settins file + if(!settings) + { + writeSettins(); + } + if(settings) + { + while(getline(settings, setting)) + { + listOfPaths.push_back(setting); + } + } + return listOfPaths; +} int main() { + std::vector listOfPaths = readSettings(); + for (int i; i < listOfPaths.size(); i++) { + std::string stringItem ="echo " + listOfPaths[1]; + const char* item = stringItem.c_str(); + system(item); + } return 0; } From 55e7247a500eeb024db4dfd5bf9b62a9297b5637 Mon Sep 17 00:00:00 2001 From: Zastian Pretorius Date: Fri, 22 Oct 2021 22:04:40 +0200 Subject: [PATCH 2/4] made Paths object and able to fill with values --- main.cpp | 72 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/main.cpp b/main.cpp index 290bf26..49a7c4e 100644 --- a/main.cpp +++ b/main.cpp @@ -7,41 +7,26 @@ namespace fs = std::filesystem; std::string settingsPath = "settings.conf"; -std::string musicPath() -{ - std::string musicFolder = ""; - return musicFolder; -} +class paths{ + public: + std::string Picturs; + std::string Music; + std::string Vidios; + std::string Arcives; +}; -std::string photoPath() -{ - std::string photoFolder = ""; - return photoFolder; -} - -std::string videoPath() -{ - std::string videoFolder = ""; - return videoFolder; -} - -std::string arcivePath() -{ - std::string arciveFolder = ""; - return arciveFolder; -} void writeSettins() { std::ofstream settings(settingsPath); - settings << "Pictures=\n$HOME/Pictures" << std::endl; + settings << "Picture=\n$HOME/Pictures" << std::endl; settings << "Music=\n$HOME/Music" << std::endl; + settings << "Video=\n$HOME/Videos" << std::endl; + settings << "Arcive=\n$HOME/Documents/Compressed" << std::endl; settings.close(); - - } + std::vector readSettings() { - std::ifstream settings(settingsPath); std::vector listOfPaths; std::string setting; @@ -59,14 +44,37 @@ std::vector readSettings() } return listOfPaths; } - +paths declarePaths(std::vector listOfPaths) +{ + paths Paths; + for (int i = 0; i < listOfPaths.size(); i+=2) + { + listOfPaths[i].pop_back(); + std::string type = listOfPaths[i]; + std::string path = listOfPaths[i+1]; + if(type == "Picture") + { + Paths.Picturs = path; + } + else if (type == "Music") + { + Paths.Music = path; + } + else if (type == "Video") + { + Paths.Vidios = path; + } + else if (type == "Arcive") + { + Paths.Arcives = path; + } + } + return Paths; +} int main() { std::vector listOfPaths = readSettings(); - for (int i; i < listOfPaths.size(); i++) { - std::string stringItem ="echo " + listOfPaths[1]; - const char* item = stringItem.c_str(); - system(item); - } + paths Paths = declarePaths(listOfPaths); + std::cout << Paths.Picturs << std::endl; return 0; } From 844bd870e0988dc5c34986fdd6b149b5f82bd509 Mon Sep 17 00:00:00 2001 From: Zastian Pretorius Date: Sat, 23 Oct 2021 00:36:08 +0200 Subject: [PATCH 3/4] make type, extentions and paths dynamic --- main.cpp | 101 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 72 insertions(+), 29 deletions(-) diff --git a/main.cpp b/main.cpp index 49a7c4e..974674b 100644 --- a/main.cpp +++ b/main.cpp @@ -4,24 +4,25 @@ #include //#include #include - +std::string musicTypes[] = {".mp3",".wav"}; +std::string pictureType[] = {".jpg","jpeg","png"}; namespace fs = std::filesystem; std::string settingsPath = "settings.conf"; -class paths{ +std::string home = getenv("HOME"); +class typeAndPaths{ public: - std::string Picturs; - std::string Music; - std::string Vidios; - std::string Arcives; + std::string type; + std::string path; + std::vector extentions; }; void writeSettins() { std::ofstream settings(settingsPath); - settings << "Picture=\n$HOME/Pictures" << std::endl; - settings << "Music=\n$HOME/Music" << std::endl; - settings << "Video=\n$HOME/Videos" << std::endl; - settings << "Arcive=\n$HOME/Documents/Compressed" << 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(); } @@ -42,39 +43,81 @@ std::vector readSettings() listOfPaths.push_back(setting); } } + settings.close(); return listOfPaths; } -paths declarePaths(std::vector listOfPaths) +std::vector declarePaths(std::vector listOfPaths) { - paths Paths; - for (int i = 0; i < listOfPaths.size(); i+=2) + typeAndPaths Paths; + std::vector paths; + for (int i = 0; i < listOfPaths.size(); i+=3) { listOfPaths[i].pop_back(); - std::string type = listOfPaths[i]; - std::string path = listOfPaths[i+1]; - if(type == "Picture") - { - Paths.Picturs = path; + Paths.type = listOfPaths[i]; + Paths.path = listOfPaths[i+1]; + std::string stringExtentions = listOfPaths[i+2]; + std::vector extentions; + char temp; + std::string tempWord; + int counter = 0; + for (int i = 0; i < stringExtentions.size(); i++) { + temp = stringExtentions[i]; + if(temp != ',') + { + + tempWord.push_back(temp); + } + else + { + extentions.push_back(tempWord); + tempWord = ""; + counter++; + } } - else if (type == "Music") + Paths.extentions = extentions; + paths.push_back(Paths); + } + return paths; +} +void checkSettingsPaths(std::vector Paths) +{ + for (int i = 0; i < Paths.size(); i++) + { + if(fs::exists(Paths[i].path)) { - Paths.Music = path; + std::cout << "Yes\n"; } - else if (type == "Video") + else { - Paths.Vidios = path; - } - else if (type == "Arcive") - { - Paths.Arcives = path; + bool valid = false; + while (!valid) { + char awnser; + std::cout << "would you like to create y=yes, n=no : " << Paths[i].path << "\t"; + std::cin >> awnser; + std::cout << '\n'; + if(tolower(awnser) == 'y') + { + fs::create_directory(Paths[i].path); + valid = true; + } + else if (tolower(awnser) == 'n') + { + std::cout << "Directory was not created\n"; + valid = true; + } + else + { + std::cout << "pleas provide valid awnser" << std::endl; + } + } } } - return Paths; } + int main() { std::vector listOfPaths = readSettings(); - paths Paths = declarePaths(listOfPaths); - std::cout << Paths.Picturs << std::endl; + std::vector Paths = declarePaths(listOfPaths); + checkSettingsPaths(Paths); return 0; } From 24138055e64fe4e916402770bfc52a37984acac4 Mon Sep 17 00:00:00 2001 From: Zastian Pretorius Date: Sat, 23 Oct 2021 02:05:58 +0200 Subject: [PATCH 4/4] 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; }