Due to some application's performance; I was required to chunk the files of a folder into subfolders. Here is the script I used for this; (using node.js)
const chunkSize = 200;
const fs = require("fs");
const path = require("path");
const inputDirectoryArg = process.argv[2];
const files = fs.readdirSync(inputDirectoryArg);
const chunks = [];
for (let i = 0; i < files.length; i += chunkSize) {
chunks.push(files.slice(i, i + chunkSize));
}
chunks.forEach((chunk, index) => {
const outputDirectory = path.join(inputDirectoryArg, `${index}`);
if (!fs.existsSync(outputDirectory)) {
fs.mkdirSync(outputDirectory);
}
chunk.forEach((file) => {
const inputFile = path.join(inputDirectoryArg, file);
const outputFile = path.join(outputDirectory, file);
console.log(`Copying ${inputFile} to ${outputFile}`);
fs.renameSync(inputFile, outputFile);
});
});
Usage: node split-folder.js <input-folder>
Edit the chunkSize
variable to change the size of the chunks.