Merge pull request #1 from ZastianPretorius/mager-rebuild

Mager Rebuild
This commit is contained in:
Zastian Pretorius
2021-10-23 02:15:41 +02:00
committed by GitHub
2 changed files with 135 additions and 174 deletions

1
.gitignore vendored
View File

@@ -5,3 +5,4 @@ tmp/
fsorter fsorter
.ccls-cache/ .ccls-cache/
CMakeFiles/ CMakeFiles/
settings.conf

300
main.cpp
View File

@@ -1,192 +1,152 @@
#include <iostream> #include <iostream>
#include <filesystem> #include <filesystem>
#include <string> #include <string>
#include <vector>
//#include <bits/stdc++.h> //#include <bits/stdc++.h>
#include <fstream> #include <fstream>
using namespace std; std::string musicTypes[] = {".mp3",".wav"};
namespace fs = filesystem; std::string pictureType[] = {".jpg","jpeg","png"};
string path; namespace fs = std::filesystem;
int numpaths; std::string home = getenv("HOME");
string searpath(){ std::string settingsPath = home+"/.config/fsorter/settings.conf";
string der; std::string sortingPath = fs::current_path();
cout<< "what is the directory you want to search?:\t"; class typeAndPaths{
cin >> der; public:
return der; std::string type;
std::string path;
std::vector<std::string> extentions;
};
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.close();
} }
// string* getsettings(){ std::vector<std::string> readSettings()
// ifstream myfile; {
// myfile.open("settings.txt"); if(!fs::exists(settingsPath))
// string str = ""; {
// int i = 0; fs::create_directory(home + "/.config/fsorter/");
// if (myfile.is_open()) {
// while (getline(myfile, str)) {
// if (str.find("^")!= string::npos){
// i += 1;
// }
// }
// myfile.close();
// }
// myfile.open("settings.txt");
// if (myfile.is_open()) {
// cout << i << "\n";
// string* fSettings = new string[i];
// i = 0;
// while (getline(myfile, str)) {
// fSettings[i] = str;
// i += 1;
// }
// numpaths = i;
// myfile.close();
// return fSettings;
// }
// }
void search() {
path = searpath();
if (fs::exists(path)) {
for (const auto &entry : fs::directory_iterator(path)) {
if (entry.path().string().find("/.") == string::npos) {
if (entry.path().has_extension()) {
cout << entry.path() << std::endl;
//// check if it is video file
if (entry.path().extension() == ".mp4" || entry.path().extension() == ".mkv") {
fs::rename(entry.path().string(), "/home/mrfluffy/Videos/" + entry.path().filename().string());
} else if (entry.path().extension() == ".jpeg" || entry.path().extension() == ".jpg" ||
entry.path().extension() == ".png") {
fs::rename(entry.path().string(),
"/home/mrfluffy/Pictures/" + entry.path().filename().string());
} }
else if (entry.path().extension() == ".mp3" ){ std::ifstream settings(settingsPath);
fs::rename(entry.path().string(), std::vector<std::string> listOfPaths;
"/home/mrfluffy/Music/" + entry.path().filename().string()); std::string setting;
// creates a default settins file
if(!settings)
{
writeSettins();
} }
else if (entry.path().extension() == ".pdf") { if(settings)
fs::rename(entry.path().string(), {
"/home/mrfluffy/Documents/PDF/" + entry.path().filename().string()); while(getline(settings, setting))
} else if (entry.path().extension() == ".docx") { {
fs::rename(entry.path().string(), listOfPaths.push_back(setting);
"/home/mrfluffy/Documents/DOCX/" + entry.path().filename().string());
} else if (entry.path().extension() == ".jar") {
fs::rename(entry.path().string(),
"/home/mrfluffy/Documents/JAR/" + entry.path().filename().string());
} else if (entry.path().extension() == ".zip" || entry.path().extension() == ".rar" ||
entry.path().extension() == ".zst" || entry.path().extension() == ".gz" || entry.path().extension() == ".xz"){
fs::rename(entry.path().string(),
"/home/mrfluffy/Documents/Compressed/" + entry.path().filename().string());
} else if (entry.path().extension() == ".iso") {
fs::rename(entry.path().string(),
"/home/mrfluffy/Documents/DiscImages/" + entry.path().filename().string());
} }
} }
} settings.close();
} return listOfPaths;
}
else{
cout << "Enter valid directory.\n";
search();
}
} }
void changepath(string nepathS){ std::vector<typeAndPaths> declarePaths(std::vector<std::string> listOfPaths)
string newpath; {
fstream myfile; typeAndPaths Paths;
myfile.open("settings.txt"); std::vector<typeAndPaths> paths;
string str = ""; for (int i = 0; i < listOfPaths.size(); i+=3)
int i = 0; {
if (myfile.is_open()) { listOfPaths[i].pop_back();
while (getline(myfile, str)) { Paths.type = listOfPaths[i];
if (str.find("^")!= string::npos){ Paths.path = listOfPaths[i+1];
i += 1; std::string stringExtentions = listOfPaths[i+2];
std::vector<std::string> 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++;
} }
} }
myfile.close(); Paths.extentions = extentions;
paths.push_back(Paths);
} }
myfile.open("settings.txt"); return paths;
if (myfile.is_open()){
cout << i << "\n";
string settings[i];
i=0;
while (getline(myfile, str)) {
settings[i] = str;
i += 1;
}
myfile.close();
ofstream temp("temp.txt");
for (int j = 0; j < i; ++j) {
if (settings[j].find(nepathS)!= string::npos){
cout << "Enter new path.:\t";
cin >> newpath;
settings[j] = nepathS + " " + newpath;
}
int t = j;
temp << settings[t] + "\n";
}
temp.close();
remove("settings.txt");
rename("temp.txt","settings.txt");
}
return;
} }
void settings(){ void checkSettingsPaths(std::vector<typeAndPaths> Paths)
// system("clear"); {
cout << "Change output location for.\n"; for (int i = 0; i < Paths.size(); i++)
cout << "1. Videos\n"; {
cout << "2. Music\n"; if(fs::exists(Paths[i].path))
cout << "3. Pictures\n"; {
cout << "4. Pdf\n"; std::cout << "Yes\n";
cout << "5. Docx\n"; }
cout << "6. Jar\n"; else
cout << "7. Disc Images\n"; {
int settingmenu; bool valid = false;
cin >> settingmenu; while (!valid) {
switch (settingmenu) { char awnser;
case 1:{ std::cout << "would you like to create y=yes, n=no : " << Paths[i].path << "\t";
changepath("^Videos"); std::cin >> awnser;
break; std::cout << '\n';
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" << Paths[i].path <<" was not created\n";
valid = true;
}
else
{
std::cout << "pleas provide valid awnser" << std::endl;
} }
case 2:
changepath("^Music");
break;
case 3:
changepath("^Pictures");
break;
case 4:
changepath("^Pdf");
break;
case 5:
changepath("^Docx");
break;
case 6:
changepath("^Jar");
break;
case 7:
changepath("^DiscIMage");
break;
} }
}
int main() {
// system("clear");
cout << "1. Sort a directory.\n";
cout << "2. settings.\n";
cout << "3. exit.\n";
int menu;
cin >> menu;
switch (menu) {
case 1:{
search();
// string* settingslist = getsettings();
// for (int i = 0; i < numpaths; ++i) {
// cout << settingslist[i] << "\n";
// }
main();
} }
case 2:
settings();
main();
case 3:
exit(3);
} }
} }
void sortPath(std::string path, std::vector<typeAndPaths> 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<std::string> listOfPaths = readSettings();
std::vector<typeAndPaths> Paths = declarePaths(listOfPaths);
checkSettingsPaths(Paths);
sortPath(sortingPath, Paths);
return 0;
}