I have an appointment where I need to go through all the files in a folder. For each file, I need to know each unique file extension, the number of files for each unique file extension, and the total size for each unique file extension. I should be able to sort this using either the file extension or the total file extension size. The first thing I thought about using was a map. This will keep track of each unique file extension and the number of times the file extension has been detected. How to associate the total file extension size with my card? So, for example, I need the result to be something like this:
Using the file extension to sort .cpp: 1: 3400 .exe: 3: 3455600 .mp4: 25: 200000404
Using the total file extension size to sort .mp4: 25: 200000404 .exe: 3: 3455600 .cpp: 1: 3400
Here is the code that I have after some editing:
#include <iostream> #include <filesystem> #include <map> using namespace std; using namespace std::tr2::sys; class fileInfo { public: int fileCount; long long fileSize; }; void scan(path f) { map<string, fileInfo> fileMap; cout << "Scanning = " << system_complete(f) << endl; directory_iterator d(f); directory_iterator e; for( ; d != e; ++d) { path p = d->path(); cout << "\nExtension is: " << extension(p) << "\tFile size is: " << file_size(p) << endl; fileMap[extension(p)].fileCount ++; fileMap[extension(p)].fileSize += file_size(p); } for (map<string, fileInfo>::iterator it = fileMap.begin(); it != fileMap.end(); ++it) { cout << it->first << " : " << it->second.fileCount << " : " << it->second.fileSize << endl; } } int main(int argc, char* argv[] ) { path folder = ".."; scan(folder); return 0; }
EDIT: So, I implemented the fileInfo file. This is a kind of work. But I have a problem with the size file. After the first run through the loop, it correctly returns file_size, but for each other run through the loop, file_size returns 0.
source share