Merge multiple files into a single one using Javascript, Typescript, or Php

Joining two or more files into a single one is an operation that may be useful sometimes when coding an application. To do it, we will read each one of the files and sequentially save their contents one after the other to a new file. The TurboDepot library FilesManager class contains the mergeFiles() method that will do this for us. Let's see how to use it with several development languages

Merge multiple files into a single one 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->mergeFiles(['path/to/file1', 'path/to/file2', 'path/to/file3'], 'path/to/dest/file4', "\n");

The call to mergeFiles() will generate a single file4 that will have the contents of file1, file2 and file3 separated with a new line character.

Merge multiple files into a single one 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.mergeFiles(['path/to/file1', 'path/to/file2', 'path/to/file3'], 'path/to/dest/file4', "\n");

The call to mergeFiles() will generate a single file4 that will have the contents of file1, file2 and file3 separated with a new line character.

Merge multiple files into a single one with Typescript (TS running on 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:

import { FilesManager } from 'turbodepot-node';
let filesManager = new FilesManager();
filesManager.mergeFiles(['path/to/file1', 'path/to/file2', 'path/to/file3'], 'path/to/dest/file4', "\n");

The call to mergeFiles() will generate a single file4 that will have the contents of file1, file2 and file3 separated with a new line character.