I need to make a program that will output all the folders and folder sizes that are in the same directory as the executable file, and when you execute it via command prompt it should display all the folders that are in the same directory as it, for example. the finished executable file of the c++ program would be able to be moved into the documents folder and all folders that were in the documents folder would be outputted when you execute the file through command prompt. what I do know is that I will probably have to make a vector or some sort of container to hold the folder names and folder sizes and output it using a ranged for loop, there are also commmand line arguments that can be used to modify how the information is outputted such as the folders are sorted alphabetically and what not. I should be able to do that, I just do not know how exactly I can go about pulling folder information in c++. If I know how exactly to get the folder names and sizes I can start doing the rest. Any help would be appreciated about how I can go about doing this.
Asked
Active
Viewed 1,381 times
0
-
While C++ can provide the containers to hold the folder (directory) names, C++ has no native directory handling functions to use (until C++17). Instead, you will use the C `opendir` and `readdir` functions with struct type `dirent`. The man pages give minimal examples on how you obtain the directory names. If you store them in a vector of strings, then you would have all your normal sorting and access features available. – David C. Rankin Apr 22 '18 at 08:03
-
1Review this: http://en.cppreference.com/w/cpp/filesystem If you can't find what you need here you probably need an Operating System specific APIs. If you do please update your question with that information. Boost also has a Filesystem library: https://www.boost.org/doc/libs/1_66_0/libs/filesystem/doc/index.htm – Richard Critten Apr 22 '18 at 08:03
-
what do you mean by the man pages? – SeePlus Apr 22 '18 at 08:18
-
On Linux `man` will lookup functions in the manual pages. If you are not on Linux then Google: "man opendir" – Richard Critten Apr 22 '18 at 08:33
-
1oh I cannot use 3rd party libraries while doing this. Is there a way I can do this without downloading a 3rd party library? – SeePlus Apr 22 '18 at 08:35
-
1Define 3rd party library please. If you have constraints on possible solutions you should include this in the question, to avoid people wasting their time. – Richard Critten Apr 22 '18 at 08:38
-
1Im sorry, I should have clarified earlier, I was a bit confused earlier but now I am fairly confident I should be using the filesystem library. – SeePlus Apr 22 '18 at 08:42
1 Answers
2
Most of what you want to do can be done using std::filesystem
in C++17
You can get the current path using current_path()
and you can then loop over everything contained in a directory using directory_iterator()
. file_size()
can be used to get the size of a file.
auto path = std::filesystem::current_path();
for (auto& obj : std::filesystem::directory_iterator(path)) {
// Get size of files
if (!std::filesystem::is_directory(obj)) {
auto size = std::filesystem::file_size(obj);
}
// Do other things
// ...
}
If you are able to use C++17 std::filesystem
is definitely the way to go.

Increasingly Idiotic
- 5,700
- 5
- 35
- 73
-
I took out the ! in your if statement because I only wanted to include folders but it seems I cannot call file_size on x after I do that? Also thank you for this it has helped me alot. – SeePlus Apr 22 '18 at 14:03
-
You can only get the file size of files, not directories. To get the size of a directory you need to recursively sum the sizes of all files in the directory. – Increasingly Idiotic Apr 22 '18 at 22:04
-
You might try looking at [this question](https://stackoverflow.com/questions/15495756/how-can-i-find-the-size-of-all-files-located-inside-a-folder) for help on finding the size of a directory – Increasingly Idiotic Apr 22 '18 at 22:05