Count the number of files and/or folders inside a directory using Javascript, Typescript, or Php
If we are developing an application that interacts with the file system, it may sometimes be necessary to count the number of items inside a directory, whether they are files, folders, or both.
To perform this operation programmatically, we normally code a recursive algorithm that loops all the directory contents and updates a counter for every one of the items found. Fortunately for us, the TurboDepot library FilesManager class has a method that does this out of the box and even lets us perform more advanced countings by using filters with regular expressions. This article will focus on the most basic operation which is to count the number of elements inside any directory using javascript, typescript, or PHP:
Count the number of items inside 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->countDirectoryItems('path/to/directory');
$filesManager->countDirectoryItems('path/to/directory', 'files');
$filesManager->countDirectoryItems('path/to/directory', 'folders');
The first call to countDirectoryItems() will return the number of files and folders that are found inside the specified directory, the second one will count the number of files ignoring the folders, and the third one will count only the number of directories ignoring the files.
Count the number of items inside a directory with Javascript (node js)
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.countDirectoryItems('path/to/directory');
filesManager.countDirectoryItems('path/to/directory', 'files');
filesManager.countDirectoryItems('path/to/directory', 'folders');
The first call to countDirectoryItems() will return the number of files and folders that are found inside the specified directory, the second one will count the number of files ignoring the folders, and the third one will count only the number of directories ignoring the files.
Count the number of items inside 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.countDirectoryItems('path/to/directory');
filesManager.countDirectoryItems('path/to/directory', 'files');
filesManager.countDirectoryItems('path/to/directory', 'folders');
The first call to countDirectoryItems() will return the number of files and folders that are found inside the specified directory, the second one will count the number of files ignoring the folders, and the third one will count only the number of directories ignoring the files.