Write a script that deletes all the regular files (not the directories) with a .js extension that are present in the current directory and all its subfolders.


    

The given script is written in Node.js, using the File System module to perform file operations.

Explanation:

1. Importing the file system module:
“`javascript
const fs = require(‘fs’);
“`
The `fs` module provides a way to interact with the file system of your computer.

2. Function to delete regular files with a .js extension:
“`javascript
function deleteJSFiles(directory) {
fs.readdir(directory, (err, files) => {
if (err) throw err;
files.forEach(file => {
const filePath = `${directory}/${file}`;

fs.stat(filePath, (err, fileStat) => {
if (err) throw err;
if (fileStat.isFile() && file.endsWith(‘.js’)) {
fs.unlink(filePath, (err) => {
if (err) throw err;
console.log(`Deleted file: ${filePath}`);
});
}
});

fs.stat(filePath, (err, fileStat) => {
if (err) throw err;
if (fileStat.isDirectory()) {
deleteJSFiles(filePath);
}
});
});
});
}
“`
The `deleteJSFiles` function takes the `directory` parameter and performs the following steps:
– It reads the content of the given `directory` using `fs.readdir`. This function takes the directory path and a callback function that receives the `err` (error) and `files` (array of file names) parameters. If an error occurs, we throw the error with `throw err`.
– Next, it loops through each `file` in the `files` array using `forEach`. For each file, we create the `filePath` by appending the file name to the given `directory`.
– We use `fs.stat` to check if `filePath` is a regular file or a directory. This function takes the file path and a callback function that receives the `err` and `fileStat` (file status) parameters. If an error occurs, we throw the error with `throw err`.
– If `fileStat.isFile()` returns `true` and the `file` ends with `.js`, we delete the file using `fs.unlink`. This function takes the file path and a callback function that receives the `err` parameter. If an error occurs, we throw the error with `throw err`. Otherwise, we log a success message to the console.
– If `fileStat.isDirectory()` returns `true`, we make a recursive call to `deleteJSFiles` with the `filePath`. This allows us to delete files in subdirectories as well.

3. Calling the deleteJSFiles function:
“`javascript
deleteJSFiles(‘./’);
“`
We call the `deleteJSFiles` function with the current directory (`’./’`). You can modify this parameter to specify a different directory if needed.

Example:

Suppose we have the following file structure in the current directory (`’./’`):

– file1.js (regular file)
– directory1 (directory)
– file2.js (regular file)
– file3.txt (regular file)
– directory2 (directory)
– file4.js (regular file)
– file5.js (regular file)
– file6.txt (regular file)

After running the script, it will delete all the regular `.js` files and output the following to the console:

“`
Deleted file: ./file1.js
Deleted file: ./directory1/file2.js
Deleted file: ./directory1/directory2/file4.js
Deleted file: ./directory1/directory2/file5.js
“`

The script successfully deleted the four regular `.js` files in the current directory and its subfolders.

Read more

Leave a comment