Welcome folks today in this blog post we will be deleting specific files with specific extensions and transfer those files to the trash
and recycle bin
in javascript. All the full source code of the application is shown below.
Get Started
In order to get started you need to initialize a new node.js
project by executing the below command as shown below
npm init -y
This will create the package.json file inside the project. Here we need to install the below libraries as shown below
npm i trash
This will install the above module where it will delete the files to recycle bin using the trash
module
Now we first of all add the type
attribute inside the package.json
file and we need to provide the value module
. This will help in using the import statement.
Now we need to copy paste the index.js
file
1 2 3 |
import trash from 'trash' await trash(['*.png']) |
As you can see we are importing the trash
module and then we are calling the trash()
method and inside it we are providing the arrray of files paths
and here we are using the pattern
of files along side with extension. Here we are selecting all the png
image files using .png extension and now if you execute the node.js
app it will delete all the png image files from the root directory.
Deleting the RecycleBin Automatically
Now we need to delete all the files from the recycle bin automatically. Many times you need to manually delete files from recycle bin. For this we need to install a node.js module which is shown below
npm i empty-trash
As you can see now we need to copy paste the index.js
file as shown below
1 2 3 |
import emptyTrash from 'empty-trash'; await emptyTrash(); |
As you can see we are importing the empty-trash
module and then we are calling the emptyTrash()
method to clear out all the files which are present inside the recycle bin.
This library also offers the cli
version of the module which is shown below.
First of all we need to install the cli library globally as shown below
npm i -g empty-trash-cli
And now we need to execute the below command to empty the trash
and recycle bin
.