Check if two directories are identical using Javascript, Typescript, or Php

Sometimes we want to compare if two directories have the same files and folders and the same size, basically checking that the contents of both are identical. To perform this check with a programming language, we usually apply a recursive approach to traverse all the directories content until a different element is found or we reach the end. Fortunately for us, the TurboDepot library FilesManager class has a method that does it really well. Let's see how we can compare any directories we want in javascript, typescript, or PHP:

Test with PHP if two directories have the same contents

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->isDirectoryEqualTo('path/to/directory1', 'path/to/directory2');

isDirectoryEqualTo() will return true if both paths are valid directories and contain exactly the same files and folders tree.

Test with Javascript (node js) if two directories have the same contents

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.isDirectoryEqualTo('path/to/directory1', 'path/to/directory2');

isDirectoryEqualTo() will return true if both paths are valid directories and contain exactly the same files and folders tree.

Test with Typescript (TS) if two directories have the same contents

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.isDirectoryEqualTo('path/to/directory1', 'path/to/directory2');

isDirectoryEqualTo() will return true if both paths are valid directories and contain exactly the same files and folders tree.