Sort Your Files Using Node.js
If you have a large number of files that need to be organized or sorted, Node.js is a great tool for automating this process. With its built-in file system module, you can easily create scripts to sort your files based on various criteria.
Getting Started
First, you’ll need to install Node.js on your computer if you haven’t already. You can download it from the official website and follow the installation instructions. Once Node.js is installed, you can start writing your file sorting scripts.
Sorting by File Type
One common way to sort files is by their file type. For example, you might want to create separate folders for images, documents, and videos. With Node.js, you can write a script that loops through all the files in a directory, identifies their file type, and moves them to the appropriate folder.
“`
const fs = require(‘fs’);
const path = require(‘path’);
const sourceDir = ‘./files’;
const imageDir = ‘./sorted_files/images’;
const documentDir = ‘./sorted_files/documents’;
const videoDir = ‘./sorted_files/videos’;
fs.readdir(sourceDir, (err, files) => {
if (err) throw err;
files.forEach(file => {
const fileType = path.extname(file).toLowerCase();
if (fileType === ‘.jpg’ || fileType === ‘.png’ || fileType === ‘.gif’) {
fs.rename(`${sourceDir}/${file}`, `${imageDir}/${file}`, (err) => {
if (err) throw err;
console.log(`Moved ${file} to ${imageDir}`);
});
} else if (fileType === ‘.pdf’ || fileType === ‘.doc’ || fileType === ‘.txt’) {
fs.rename(`${sourceDir}/${file}`, `${documentDir}/${file}`, (err) => {
if (err) throw err;
console.log(`Moved ${file} to ${documentDir}`);
});
} else if (fileType === ‘.mp4’ || fileType === ‘.avi’ || fileType === ‘.mov’) {
fs.rename(`${sourceDir}/${file}`, `${videoDir}/${file}`, (err) => {
if (err) throw err;
console.log(`Moved ${file} to ${videoDir}`);
});
}
});
});
“`
Sorting by Date
Another common way to sort files is by their creation or modification date. This can be useful for organizing files into folders based on the year, month, or day they were created or modified. Node.js provides methods for accessing file metadata, making it easy to sort files based on their dates.
“`
const fs = require(‘fs’);
const path = require(‘path’);
const sourceDir = ‘./files’;
const sortedDir = ‘./sorted_files’;
fs.readdir(sourceDir, (err, files) => {
if (err) throw err;
files.forEach(file => {
const stats = fs.statSync(`${sourceDir}/${file}`);
const year = stats.mtime.getFullYear();
const month = stats.mtime.getMonth() + 1;
const day = stats.mtime.getDate();
const folderPath = `${sortedDir}/${year}/${month}/${day}`;
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
fs.rename(`${sourceDir}/${file}`, `${folderPath}/${file}`, (err) => {
if (err) throw err;
console.log(`Moved ${file} to ${folderPath}`);
});
});
});
“`
Conclusion
Node.js provides a powerful and flexible platform for sorting files. With its file system module and easy-to-use syntax, you can create custom file sorting scripts to meet your specific needs. Whether you want to sort files by type, date, or any other criteria, Node.js has you covered.
So why not give it a try and start organizing your files with Node.js today?