-1

I am new to file reading, and my question is, how to determine files/folders present within another folder?

Example:

A folder, "whoa" contains the following files:

+ whoa

    - hello.dll

    - world.dll

    - helloworld.exe

    + cplusplus //cplusplus is a folder

        - c++.png

Now, I want to determine the contents of "whoa" through c++, how would I do that? Also, I want to create treeview of the contents as well.

codectile
  • 11
  • 1
  • 5
  • 2
    There is not a standard way to do that in C++, AFAIK. Which OS are you on? – Banex Aug 01 '15 at 11:09
  • 3
    Not standard, but `boost::filesystem ` is widely portable and useful for this case. – πάντα ῥεῖ Aug 01 '15 at 11:11
  • possible duplicate of [How can I get the list of files in a directory using C or C++?](http://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c) – fvu Aug 01 '15 at 11:12
  • @codectile If you're not looking for portability, and happen to use Visual Studio 2012 (or later), there is a `` header that does what you want. – Banex Aug 01 '15 at 11:15

1 Answers1

0

There is no portable way to do that in standard C++. There are plans to standardize file system operations in the new iteration of the C++ standard.

Not portable, using <filesystem> header available in VS2012+. Incidentally, the classes are very similar to those of boost::filesystem.

std::vector<string> filePaths;
path folderPath = "whoa";

if (is_directory(folderPath))
{
    // This recursively traverses the folder structure.
    // Use directory_iterator if just want to traverse the current folder.
    recursive_directory_iterator endit;
    recursive_directory_iterator it(folderPath);

    for (; it != endit; ++it)
    {
        string filePath = it->path().string();
        filePaths.push_back(filePath);
    }
}

If you want portability, take a look at boost::filesystem.

Banex
  • 2,890
  • 3
  • 28
  • 38
  • Will it determine files with various extensions? – codectile Aug 01 '15 at 11:31
  • @codectile `filePaths` is a vector that is going to be filled with all the file and folder paths contained in folder `whoa`. And the path includes the extension. – Banex Aug 01 '15 at 11:32