How to Dump Files in Individual Folders

23 March , 2014 Bill C++ Programming

Introduction
While installing the Boxee+ hack on my media center I found out that all my movies needed to be saved in individual folders of the same name. This needs to be done for Boxee+ and XBMC to display fanart. With thousands of files, this was going to be a huge manual task. After searching for a tool to complete this task I decided to make one.

Usage
folder_dump
-p : verify paths
-e : verify each file
-i : include hidden files
-d : don’t remove file extensions for folder names
-v : display each file name as completed
-h : help

Code

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <sys/stat.h>
#include <cstdio>

using namespace std;

int main(int argc, char *argv[])
{
    bool verify_path = false;
    bool verify_each = false;
    bool include_hidden = false;
    bool dont_remove_extension = false;
    bool verbose = false;
    string source_path = "";
    string destination_path = "";

    for(int i = 1; i <= argc-1; i++)
    {
        if(argv[i][0] == '-')
        {
            if(strcmp(argv[i], "-p") == 0) verify_path = true;
            if(strcmp(argv[i], "-e") == 0) verify_each = true;
            if(strcmp(argv[i], "-i") == 0) include_hidden = true;
            if(strcmp(argv[i], "-d") == 0) dont_remove_extension = true;
            if(strcmp(argv[i], "-v") == 0) verbose = true;
            if(strcmp(argv[i], "-h") == 0)
            {
                cout << "folder_dump <flags> <source path> <destination path>" << endl;
                cout << "-p : verify paths" << endl;
                cout << "-e : verify each file" << endl;
                cout << "-i : include hidden files" << endl;
                cout << "-d : don't remove file extensions for folder names" << endl;
                cout << "-v : display each file name as completed" << endl;
                cout << "-h : help" << endl;
                return 0;
            }
    }
    else
    {
        if(source_path.compare("") == 0)
            source_path = argv[i];
        else
            destination_path = argv[i];
        }
    }
    if(verify_path == true)
    {
        string cin_continue = "";
        cout << "source path : " << source_path << endl;
        cout << "destination path : " << destination_path << endl;
        cout << "continue [y/n] :";
        cin >> cin_continue;
        if(cin_continue.compare("y") !=0)
        {
            cout << "canceled" << endl;
            return 0;
        }
    }
    if(source_path.compare("") == 0)
    {
        cout << "error: source path was not provided : (try -h for help)" << endl;
        return -101;
    }
    if(destination_path.compare("") == 0)
    {
        cout << "error: destination path was not provided : (try -h for help)" << endl;
        return -102;
    }
    if(source_path[strlen(source_path.c_str()) - 1] != '/') source_path += "/";
    if(destination_path[strlen(destination_path.c_str()) - 1] != '/') destination_path += "/";

    // - - - - - - - - - - - -
    // DO THE WORK
    DIR *dp;
    struct dirent *dirp;
    if((dp = opendir(source_path.c_str())) == NULL)
    {
        cout << "error: (" << errno << ") opening " << source_path << endl;
        return errno;
    }
    while ((dirp = readdir(dp)) != NULL)
    {
        if(dirp->d_type == DT_REG)
        {
            if((dirp->d_name[0] == '.' && include_hidden == true) || dirp->d_name[0] != '.')
            {
                string folder_name = "";
                for(int i = 0; i <= strlen(dirp->d_name) - 1; i++)
                {
                    if(dirp->d_name[i] == '.' && i == 0) {} else folder_name += dirp->d_name[i];
                }
                if(dont_remove_extension == false)
                {
                    int pos = folder_name.find_last_of(".");
                    if(pos != string::npos) folder_name = folder_name.substr(0, pos);
                }
                string source_file = source_path + dirp->d_name;
                string new_folder = destination_path + folder_name;
                string destination_file = new_folder + "/" + dirp->d_name;

                bool skip = false;

                if(verify_each == true)
                {
                    string cin_process = "";
                    cout << "process: " << dirp->d_name << " [y/n]";
                    cin >> cin_process;
                    if(cin_process.compare("y") !=0) skip = true;
                }

                if(skip == false)
                {
                    try
                    {
                        mkdir(new_folder.c_str(), S_IRWXU | S_IROTH | S_IRGRP | S_IXOTH);
                    }
                    catch(...)
                    {
                        cout << "error: creating new folder. check the destination path is correct.";
                        cout << "source path : " << source_path << endl;
                        cout << "destination path : " << destination_path << endl;
                        cout << "current file : " << source_file << endl;
                        cout << "current destination : " << destination_file << endl;
                        return -103;
                    }
                    try
                    {
                        rename(source_file.c_str(), destination_file.c_str());
                    }
                    catch(...)
                    {
                        cout << "error: creating new folder. check the destination path is correct.";
                        cout << "source path : " << source_path << endl;
                        cout << "destination path : " << destination_path << endl;
                        cout << "current file : " << source_file << endl;
                        cout << "current destination : " << destination_file << endl;
                        return -104;
                    }
                    if(verbose == true)
                    {
                        cout << "moved : " << dirp->d_name << endl;
                    }
                }
            }
        }
    }
    closedir(dp);
    return 0;
}

Compiling the Code
Copy and save the code to a file with the name “main.cpp”. Open a terminal window and use cd to change to the directory of the file. Use the following command to compile the code to an executable.

g++ main.cpp -o folder_dump

how to, linux, programming, tutorial,

4 Responses to “How to Dump Files in Individual Folders”


Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress. Designed by elogi.