Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <dirent.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <vector>
- #include <errno.h>
- using namespace std;
- //Returns the file name separated from it's extension
- string get_filename(string full_file_name) {
- string filename;
- int dotPos = full_file_name.find_last_of(".");
- if (dotPos > 0) {
- if (full_file_name == "." && full_file_name == "..") { //handling . and .. files
- filename = full_file_name;
- }
- else {
- filename = full_file_name.substr(0, dotPos); //file name is everything going before the last dot in full file name
- }
- }
- else {
- filename = full_file_name; //if full file name contains no dots then it has no extension
- }
- return filename;
- }
- //Returns the file extension separated from it's name
- string get_extension(string full_file_name) {
- string extension = "";
- int dotPos = full_file_name.find_last_of(".");
- if (dotPos > 0) {
- if (full_file_name != "." && full_file_name != "..") { //avoiding files . and .. 'cause they have no extension
- extension = full_file_name.substr(dotPos + 1);
- }
- }
- return extension;
- }
- //Removes file from filesystem.
- //Arguments:
- // * full_file_name - full name of the file to remove (name and extension)
- // * path_to_file - path to the file to remove (without file name and extension)
- // * show_errors - if set to true, then function will output all the errors occured while removing files
- void remove_file(string full_file_name, string path_to_file, bool show_errors) {
- string full_path = path_to_file + "/" + full_file_name;
- const char* temp = full_path.c_str();
- if (remove(temp) != 0 && show_errors == true) {
- cout << "Can't remove " << full_path << ": " << strerror(errno) << endl;
- }
- }
- int main (int argc, char* argv[]) {
- if (argc == 2 && string(argv[1]) == "--help" || string(argv[1]) == "-h") {
- cout << "Usage: ./task19 DIRECTORY... EXTENSION..." << endl;
- cout << "Removes from the DIRECTORY all the files with equal filenames except those which extension is EXTENSION." << endl;
- return 0;
- }
- if (argc < 3) {
- cout << "Missing some arguments. Try './task19 --help' for more information." << endl;
- return 0;
- }
- string dir_path = argv[1];
- string extension = argv[2];
- bool show_errors = false;
- if (argc > 3 && string(argv[3]) == "-er") {
- show_errors = true;
- }
- vector<string> filelist;
- DIR* dir_pointer = NULL;
- dir_pointer = opendir(dir_path.c_str());
- struct dirent* pent = NULL;
- //saving list of files in the directory to vector
- while (pent = readdir (dir_pointer)) {
- filelist.push_back(pent->d_name);
- }
- closedir(dir_pointer);
- for (int i = 0; i < filelist.size(); ++i) {
- string xname = get_filename(filelist[i]);
- string xext = get_extension(filelist[i]);
- for (int j = 0; j < filelist.size(); ++j) {
- string yname = get_filename(filelist[j]);
- string yext = get_extension(filelist[j]);
- if (xname == yname && filelist[i] != filelist[j]) {
- if (xext != extension) {
- remove_file(filelist[i], dir_path, show_errors);
- }
- if (yext != extension) {
- remove_file(filelist[j], dir_path, show_errors);
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement