# Finding size of a directory

To find the size of a directory we use a simple recursive algorithm. The algorithm works by iterating through the contents of the directory; if the file inside the directory is a regular file we add the size of the file to the directory size. If the file is a directory we use the same function which iterates through the content of this directory. Thus we use a recursive logic.

Algorithm : FindDirSize(Directory dir)
0.   Initialize directory size sz to 0.
1.   Iterate through the content of the directory.
foreach file inside the dir do
2.             if the file is a regular file then
sz += size of regular file.
3.             else if the file is a directory (childdir) then
sz += FindDirSize (childdir).
4.             return sz

C++ : Finding size of a directory using recursion. Implemented in C++14

``````#include<iostream>
#include<vector>
#include<list>
#include<map>

using namespace std;

class File {

public:
char type;
string name;
int size;

File(char arg_type, string arg_name, int arg_size) : type(arg_type), name(arg_name), size(arg_size)
{ }
// Instead of writing a Compare class wth operator() overloaded,
// we can overload < operator in the File class as below
/*
bool operator< (const File & f) const {
if (f.name < this->name)
return true;
}*/
};

class CompareFileName {

public:
bool operator() (const File & a, const File & b) const {
return (a.name < b.name);
}
};

class FileSystem {

private:
map<File, vector<File>, CompareFileName> fs;

public:

FileSystem(map<File, vector<File>, CompareFileName> arg_fs) : fs(arg_fs)
{}

int FindDirSize(File dir) {
int sz = 0;
for (const auto& fileobj : fs[dir]) {
if (fileobj.type == 'D') {
sz += FindDirSize(fileobj);
} else {
sz += fileobj.size;
}
}
return sz;
}
};

int main(){

map<File, vector<File>, CompareFileName> fs;

File root('D', "Root", 0);

File d1('D', "D1", 0);
File f1('F', "F1", 1);
File f2('F', "F2", 2);
File f3('F', "F3", 3);

File d2('D', "D2", 0);
File f4('F', "F4", 4);
File f5('F', "F5", 5);
File f6('F', "F6", 6);

File d3('D', "D3", 0);
File f7('F', "F7", 7);
File f8('F', "F8", 8);

File d4('D', "D4", 0);
File f9('F', "F9", 9);

File f10('F', "F10", 10);

vector<File> inside_d1 = { f1, f2, f3 };
fs.insert({d1, inside_d1});

vector<File> inside_d2 = { f4, f5, f6 };
fs.insert({d2, inside_d2});

vector<File> inside_d3 = { f7, f8, d4 };
fs.insert({d3, inside_d3});

vector<File> inside_d4 = { f9 };
fs.insert({d4, inside_d4});

vector<File> inside_root = { d1, d2, d3, f10 };
fs.insert({root, inside_root});

FileSystem fsobj(fs);
cout << "Size of root directory : " << fsobj.FindDirSize(root) << endl;
cout << "Size of d1 directory : " << fsobj.FindDirSize(d1) << endl;
cout << "Size of d2 directory : " << fsobj.FindDirSize(d2) << endl;
cout << "Size of d3 directory : " << fsobj.FindDirSize(d3) << endl;
cout << "Size of d4 directory : " << fsobj.FindDirSize(d4) << endl;

return 0;
}
``````

Output of finding the size of a directory using recursion. Implemented in C++14.

``````Size of root directory : 55
Size of d1 directory : 6
Size of d2 directory : 15
Size of d3 directory : 24
Size of d4 directory : 9
``````
Copyright (c) 2020-2021, Algotree.org.