"Clear the Clutter" is a Node.js script designed to organize files in a specified directory by grouping them into subdirectories based on their file extensions. This helps in maintaining a clean and organized file structure.
- Automatically reads the contents of a specified directory.
- Creates subdirectories for each file extension if they do not already exist.
- Moves files into their respective subdirectories based on their extensions.
- Provides console logs to indicate the progress and completion of the file organization process.
- Node.js (v14 or later)
- npm (Node Package Manager)
-
Clone the repository:
git clone https://github.com/HackesticMedusa/clear_the_clutter.git cd clear_the_clutter
-
Install dependencies:
This project does not have any external dependencies, so no need to run
npm install
.
-
Update the directory path:
Open
organizeFiles.cjs
and update thedirectoryPath
variable to the path of the directory you want to organize.const directoryPath = 'path_to_your_directory';
-
Run the script:
node organizeFiles.cjs
-
Output:
The script will log the progress of moving files to the console. For example:
Moved file1.txt to txt/ Moved image1.jpg to jpg/ Files organized successfully.
The script is written in JavaScript and uses Node.js built-in modules fs
and path
.
-
Reading Directory:
const files = await fs.promises.readdir(dirPath);
-
Checking File Type:
const fileStat = await fs.promises.stat(filePath); if (fileStat.isFile()) { // Process file }
-
Creating Subdirectories:
if (!fs.existsSync(extDir)) { await fs.promises.mkdir(extDir); }
-
Moving Files:
await fs.promises.rename(filePath, newFilePath);
The script includes a try-catch block to handle any errors that may occur during the file organization process. Errors will be logged to the console.
catch (error) {
console.error('Error organizing files:', error);
}
This project is licensed under the MIT License. See the LICENSE file for details.
- Ankur Karmakar aka HackesticMedusa.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
- Node.js documentation for providing comprehensive guides and examples.