Advertisement
poohitan

Untitled

Mar 13th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <dirent.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <vector>
  7. #include <errno.h>
  8.  
  9. using namespace std;
  10.  
  11. //Returns the file name separated from it's extension
  12. string get_filename(string full_file_name) {
  13.   string filename;
  14.  
  15.   int dotPos = full_file_name.find_last_of(".");
  16.   if (dotPos > 0) {
  17.     if (full_file_name == "." && full_file_name == "..") { //handling . and .. files
  18.       filename = full_file_name;
  19.     }
  20.     else {    
  21.       filename = full_file_name.substr(0, dotPos); //file name is everything going before the last dot in full file name
  22.     }
  23.   }
  24.   else {  
  25.     filename = full_file_name; //if full file name contains no dots then it has no extension
  26.   }
  27.  
  28.   return filename;
  29. }
  30.  
  31. //Returns the file extension separated from it's name
  32. string get_extension(string full_file_name) {
  33.   string extension = "";
  34.  
  35.   int dotPos = full_file_name.find_last_of(".");
  36.   if (dotPos > 0) {
  37.     if (full_file_name != "." && full_file_name != "..") { //avoiding files . and .. 'cause they have no extension
  38.       extension = full_file_name.substr(dotPos + 1);
  39.     }
  40.   }
  41.  
  42.   return extension;
  43. }
  44.  
  45. //Removes file from filesystem.
  46. //Arguments:
  47. //  * full_file_name - full name of the file to remove (name and extension)
  48. //  * path_to_file - path to the file to remove (without file name and extension)
  49. //  * show_errors - if set to true, then function will output all the errors occured while removing files
  50. void remove_file(string full_file_name, string path_to_file, bool show_errors) {
  51.   string full_path = path_to_file + "/" + full_file_name;
  52.  
  53.   const char* temp = full_path.c_str();
  54.   if (remove(temp) != 0 && show_errors == true) {
  55.     cout << "Can't remove " << full_path << ": " << strerror(errno) << endl;
  56.   }
  57. }
  58.  
  59. int main (int argc, char* argv[]) {
  60.   if (argc == 2 && string(argv[1]) == "--help" || string(argv[1]) == "-h") {
  61.     cout << "Usage: ./task19 DIRECTORY... EXTENSION..." << endl;
  62.     cout << "Removes from the DIRECTORY all the files with equal filenames except those which extension is EXTENSION." << endl;
  63.     return 0;
  64.   }
  65.  
  66.   if (argc < 3) {
  67.     cout << "Missing some arguments. Try './task19 --help' for more information." << endl;
  68.     return 0;
  69.   }
  70.  
  71.   string dir_path = argv[1];
  72.   string extension = argv[2];
  73.   bool show_errors = false;
  74.  
  75.   if (argc > 3 && string(argv[3]) == "-er") {
  76.     show_errors = true;
  77.   }
  78.  
  79.   vector<string> filelist;
  80.  
  81.   DIR* dir_pointer = NULL;
  82.   dir_pointer = opendir(dir_path.c_str());
  83.   struct dirent* pent = NULL;  
  84.  
  85.   //saving list of files in the directory to vector
  86.   while (pent = readdir (dir_pointer)) {
  87.     filelist.push_back(pent->d_name);
  88.   }
  89.  
  90.   closedir(dir_pointer);
  91.  
  92.   for (int i = 0; i < filelist.size(); ++i) {
  93.     string xname = get_filename(filelist[i]);
  94.     string xext = get_extension(filelist[i]);
  95.  
  96.     for (int j = 0; j < filelist.size(); ++j) {
  97.       string yname = get_filename(filelist[j]);
  98.       string yext = get_extension(filelist[j]);
  99.  
  100.       if (xname == yname && filelist[i] != filelist[j]) {
  101.         if (xext != extension) {
  102.           remove_file(filelist[i], dir_path, show_errors);
  103.         }
  104.         if (yext != extension) {
  105.           remove_file(filelist[j], dir_path, show_errors);
  106.         }
  107.       }
  108.     }
  109.   }
  110.  
  111.   return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement