0

I'm having trouble implementing the top answer here: How to get list of files with a specific extension in a given folder

I am attempting to get all of the ".vol" files in the directory argv[2] and do some batch processing with each file that I find. I want to pass each file to the ParseFile function which takes a string as an argument.

// return the filenames of all files that have the specified extension
// in the specified directory and all subdirectories
vector<string> get_all(const boost::filesystem::path& root, const string& ext, vector<boost::filesystem::path>& ret){
    if(!boost::filesystem::exists(root) || !boost::filesystem::is_directory(root)) return vector<string>();

    boost::filesystem::recursive_directory_iterator it(root);
    boost::filesystem::recursive_directory_iterator endit;

    while(it != endit)
    {
        if(boost::filesystem::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename());
        ++it;
        cout << *it << endl; 
        return *ret;   // errors here
    }
}



... main function


if (batch) {
   vector<boost::filesystem::path> retVec;
   vector<boost::filesystem::path> volumeVec = get_all(boost::filesystem::path(string(argv[2])), string(".vol"), retVec);


// convert volume files in volumeVec to strings and pass to ParseFile
   ParseFile(volumeFileStrings);

}

I am having trouble with the get_all function and how to return the vector correctly.

Community
  • 1
  • 1
user2007843
  • 609
  • 1
  • 12
  • 30
  • Add more detail. "I am having trouble with the get_all function and how to return the vector correctly" - what problem(s) *specifically*? What have you tried? What results did you get? What did you expect instead? Also, rather than "... main function" you should form your question in the form of a [SSCCE](http://sscce.org) so that other people can test and reproduce your results/problems. – Jesper Juhl Apr 05 '17 at 18:33
  • I'm getting error: no match for 'operator*' (operand type is 'std::vector') return * ret – user2007843 Apr 05 '17 at 18:35
  • Why are you returning with `return *ret` in the middle of your `while` loop – Andria Apr 05 '17 at 18:39
  • I think if you move the `return` statement outside of the while loop you should be fine. – Andria Apr 05 '17 at 18:41
  • Also the variable `ret` needs to be a `vector` and shouldn't be passed in if you're returning it, if you're returning it create it in your code like such `vector ret;` otherwise change the parameter of the function to `vector& ret){` – Andria Apr 05 '17 at 18:43
  • @chbchb55 still getting the same error. In addition I'm getting this error : error: conversion from ‘std::vector >’ to non-scalar type ‘std::vector’ requested vector VolumeVec = get_all(boost::filesystem::path(string(argv[2])), string(".vol"), retVec); ^ – user2007843 Apr 05 '17 at 18:43
  • ill try what you just suggested and see what happens – user2007843 Apr 05 '17 at 18:44
  • Actually if the code `it->path().filename()` returns a `boost::filesystem::path` then your return statement just has to be the same or if you would like to keep the parameter to pass in a vector just change the return statement to void – Andria Apr 05 '17 at 18:48
  • I don't care to pass in a vector, I just want to return a vector with the filenames so the former option would be what I'm looking for. I removed the vector from the function definition and just put it in the function. When I try to return `it->path().filename()` I get error: could not convert boost::filesystem::path::filename() const()' from 'boost::filesystem::path' to 'std::vector >' – user2007843 Apr 05 '17 at 18:58
  • 1
    Wait, I think I just need to change my function defintion from a vector to a vector – user2007843 Apr 05 '17 at 19:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/140010/discussion-between-user2007843-and-chbchb55). – user2007843 Apr 05 '17 at 19:44

1 Answers1

1

Change return statement to vector<boost::filesystem::path> and remove ret from the parameters for the function and instead create ret in the function like so:

vector<boost::filesystem::path> ret;

Then you'll want to move the return statement of ret, return ret;, below the while loop so it appends all of the file names to ret.

Your code will look something like this:

vector<boost::filesystem::path> get_all(const boost::filesystem::path& root, const string& ext){
    if(!boost::filesystem::exists(root) || !boost::filesystem::is_directory(root)) return;

    boost::filesystem::recursive_directory_iterator it(root);
    boost::filesystem::recursive_directory_iterator endit;
    vector<boost::filesystem::path> ret;
    while(it != endit)
    {
        if(boost::filesystem::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename());
        ++it;
        cout << *it << endl; 
    }
    return ret;
}
Andria
  • 4,712
  • 2
  • 22
  • 38