Calculate the total size of a directory using Javascript, Typescript, or Php

When developing an application is fairly common to operate with directories and files and sometimes we may need to obtain the full space that a directory is using in our file system. To perform this check with a programming language, we usually apply a recursive approach to loop through all the files in the folder and add their size to the total value. Fortunately for us, with the TurboDepot FilesManager class we can perform this process by calling a single method. Let's see how we can compute the size of any directory using Javascript, Typescript, or PHP:

Calculate the total size of a directory with PHP

Download the latest TurboCommons and TurboDepot phar files, place them on your project as dependencies and run the following code:

require 'path/to/your/dependencies/folder/turbocommons-php-X.X.X.phar';
require 'path/to/your/dependencies/folder/turbodepot-php-X.X.X.phar';

use org\turbodepot\src\main\php\managers\FilesManager;

$filesManager = new FilesManager();
$filesManager->getDirectorySize('path/to/directory');

getDirectorySize() will return an integer with the total size of the directory in bytes (if the provided path is valid).

Calculate the total size of a directory with Javascript (nodeJs)

Install the turbodepot dependency by executing the following command at the root of your project:

npm install turbodepot-node

And then run the following code:

const { FilesManager } = require('turbodepot-node');
let filesManager = new FilesManager();
filesManager.getDirectorySize('path/to/directory');

getDirectorySize() will return an integer with the total size of the directory in bytes (if the provided path is valid).

Calculate the total size of a directory with Typescript (TS)

Install the turbodepot dependency by executing the following command at the root of your project:

npm install turbodepot-node

And then run the following code:

import { FilesManager } from 'turbodepot-node';
let filesManager = new FilesManager();
filesManager.getDirectorySize('path/to/directory');

getDirectorySize() will return an integer with the total size of the directory in bytes (if the provided path is valid).