0

I want to get a list of files in a folder, if there are any subfolders inside, I want the list of files within these subfolders, too.

I have read the question How can I get the list of files in a directory using C or C++?
but it didn't help me solve all the problem.

All I want is a function like this:

vector<string> fileList;

//return the number of files within a folder.
// @ dir: the folder directory
// @ list: the output list of files
// @ filetype: could be multiple similar types

int getFileListFrom(const string & dir, vector<string>:: list, 
    const string & filetype);
int getFileListFrom(const string & dir, vector<string>:: list, 
    const vector<string> & filetypes);

I hope this function can be used as follows (hypothetically):

//Example1
fileNum1 = getFileListFrom("C:\\Program Files(x69)\\PornoX", list1, "avi");

//Example2
fileNum2 = getFileListFrom("D:\\myWorkspace\\Documentations\\", list2,
  vector<string>("doc","docx","pdf","txt"));

I'm using Windows x86 system, so don't worry about cross-platform issue. Also, if any 3rd-part packages are necessary, please provide download links and documentations.

Any help will be deeply appreciated!

Community
  • 1
  • 1
Lotayou
  • 162
  • 11
  • You could use a [`recursive_directory_iterator`](http://en.cppreference.com/w/cpp/filesystem/recursive_directory_iterator). – Kerrek SB Apr 25 '16 at 08:08
  • And if your compiler is to old to include [the (upcoming) standard filesystem library](http://en.cppreference.com/w/cpp/filesystem) you could use [Boost filesystem](http://www.boost.org/doc/libs/1_60_0/libs/filesystem/doc/index.htm). – Some programmer dude Apr 25 '16 at 08:11
  • Carefully read all answers to the question you linked to: it can definitely be used to solve your problem. – stijn Apr 25 '16 at 08:11
  • I think this answer is what you search for: http://stackoverflow.com/a/20847429/1212012 – Mathieu Apr 25 '16 at 08:15
  • [This wiki book chapter](https://github.com/angrave/SystemProgramming/wiki/File-System%2C-Part-4%3A-Working-with-directories) as part of a bigger wiki book that provides a good introduction to lots of C systems programming concepts, including navigating through a unix-like filesystem, might be a good place to learn about this filesystem stuff in C. – user3773048 Apr 25 '16 at 08:21

0 Answers0