added file sorting and make settings in ./config folder

This commit is contained in:
Zastian Pretorius
2021-10-23 02:05:58 +02:00
parent 844bd870e0
commit 24138055e6

View File

@@ -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<std::string> readSettings()
{
if(!fs::exists(settingsPath))
{
fs::create_directory(home + "/.config/fsorter/");
}
std::ifstream settings(settingsPath);
std::vector<std::string> listOfPaths;
std::string setting;
@@ -98,11 +103,12 @@ void checkSettingsPaths(std::vector<typeAndPaths> 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<typeAndPaths> Paths)
}
}
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;
}