如何加快递归文件搜索(文件系统c ++)

时间:2017-04-14 05:06:32

标签: c++ boost filesystems

编写一个程序,根据提供的扩展名扫描硬盘驱动器中的文件。我正在测试的当前版本是“-c + r D://”这将从D:驱动器的根目录开始递归查找c,c ++文件。然而,这是非常缓慢的,我不知道我可以做些什么来加快速度。

#include <map>
#include <iostream>
#include <string>
using namespace std;

#include <filesystem>
using namespace std::experimental::filesystem;


//class wide booleans
bool cFiles, cPlusFiles, javaFiles, cSharpFiles, webProgrammingFiles;
bool bool_summary, bool_recursive, bool_reverse, bool_sort, bool_verbose;
bool show_help;
string regex = "";
//regex


// extension       file size
map<string, vector<long long>> extensionMap;//map which stores all files
vector<long long> fileSizes;

//searches condition string and ticks off booleans which will be used in file search
void check_conditions(string conditions) {
    for (unsigned int i = 1; i < conditions.size(); ++i)
    {
        switch (conditions.at(i))
        {
        case 'c':
            cFiles = true;
            break;
        case '+':
            cPlusFiles = true;
            break;
        case 'j':
            javaFiles = true;
            break;
        case '#':
            cSharpFiles = true;
            break;
        case 'w':
            webProgrammingFiles = true;
            break;
        case 's':
            bool_summary = true;
            break;
        case 'x':
            //while (conditions.at(i) != '"')
            break;
        case 'r':
            bool_recursive = true;
            break;
        case 'R':
            bool_reverse = true;
            break;
        case 'S':
            bool_sort = true;
            break;
        case 'v':
            bool_verbose = true;
            break;
        case 'h':
            show_help = true;
            break;
        default:
            break;
        }
    }
}

// scan a single folder
void scan(path const& f) {
    directory_iterator d(f);    // first entry of folder 'f'
    directory_iterator e;       // virtual match to the 'end' of any folder
    string extension;
    while (d != e) {
        cout << d->path() <<
            (is_directory(d->status()) ? " [dir]" : "") <<
            " ext=" << d->path().extension() <<
            endl;
        extension = d->path().extension().string();

        if (extensionMap.find(extension) == extensionMap.end())
        {
            //not found
        }
        else
        {
            extensionMap[extension].push_back(file_size(d->path()));
        }

        ++d;
    }

}

// scan a current folder and all sub folders
void rscan(path const& f) {
    cout << "\n";
    string extension = "";
    //int testsize;
    for (recursive_directory_iterator d(f), e; d != e; ++d) {
        /*cout << d->path() <<
            (is_directory(d->status()) ? " [dir]" : "") <<
            " ext=" << d->path().extension() <<
            endl;*/
        extension = d->path().extension().string();

        if (extension == ".cpp")
        {
            cout << "CPP!!!!" << endl;
        }


        if (extensionMap.find(extension) == extensionMap.end())
        {
            //not found
        }
        else
        if (!is_directory(d->path()))//makes sure its not a folder
        {
            //testsize = file_size(d->path());
            //cout << d->path() << endl;
            //cout << "size is " << file_size(d->path()) << endl;
            extensionMap[extension].push_back(file_size(d->path()));
        }
    }
}


int main(int argc, char* argv[])
{
    cout.imbue(locale(""));
    //if (argc )
    string conditions = "-c+r";
    string directory = "";
    path finalDirectory;
    cout << canonical(finalDirectory);

    cout << endl;
    //string directory = argv[2];

    cout << "fileusage (c) 2016-7, Hur Romanchik" << endl;

    cout << "argc is " << argc << endl;

    for (int i = 0; i < argc; ++i)
    {
        cout << argv[i] << endl;
    }

    if (argc > 1) {

        if (conditions.at(0) == '-' && argc > 2)
        {
            check_conditions(conditions);
            directory = argv[2];
        }
        else
        {
            directory = argv[1];
        }


        cout << "Directory is " << directory << endl;


        if (show_help == true) //show help and exit if user asks for help
        {
            cout << "Usage: fileusage [-hrRsSvc+#jw(x regularexpression)] [folder]" << endl;
            return 0; //exit
        }

        if (cFiles == true)
        {
            cout << "C files: " << directory << endl;
            extensionMap.insert(pair<string, vector<long long>>(".c", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".h", fileSizes));
        }
        if (cPlusFiles == true)
        {
            cout << "C++ files: " << directory << endl;
            extensionMap.insert(pair<string, vector<long long>>(".cc", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".cp", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".cpp", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".cxx", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".c++", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".hpp", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".hxx", fileSizes));
        }
        if (cSharpFiles == true)
        {
            cout << "C# files: " << directory << endl;
            extensionMap.insert(pair<string, vector<long long>>(".cs", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".vb", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".jsl", fileSizes));
        }
        if (javaFiles == true)
        {
            cout << "Java files: " << directory << endl;
            extensionMap.insert(pair<string, vector<long long>>(".class", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".jar", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".java", fileSizes));
        }
        if (webProgrammingFiles == true)
        {
            cout << "Web Programming Files";
            extensionMap.insert(pair<string, vector<long long>>(".htm", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".html", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".html5", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".js", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".jse", fileSizes));
            extensionMap.insert(pair<string, vector<long long>>(".jsc", fileSizes));
        }
        cout << "Creatiiong cpmp" << endl;
    }
    else
    {
        finalDirectory = current_path();//sets current path as search
    }
    if (directory.empty() == false)
        finalDirectory = directory;

    if (bool_recursive == true)
        rscan(finalDirectory);
    else
        scan(finalDirectory);

    cout << "scan complete" << endl;

    for (map<string, vector<long long>>::const_iterator it = extensionMap.begin(); it != extensionMap.end(); ++it)
    {
        //cout << it->first << endl;
        //cout << extensionMap[it->first].size();
        cout << "# of " << it->first << " files is " << extensionMap[it->first].size();
        for (vector<long long>::size_type i = 0; i < extensionMap[it->first].size(); ++i)
        {
            cout << it->first << " file is " << extensionMap[it->first].at(i) << " bytes" << endl;

        }
    }


    system("pause");
}

0 个答案:

没有答案
相关问题