Home  >  Article  >  Web Front-end  >  nodejs+ file regularly deletes files

nodejs+ file regularly deletes files

WBOY
WBOYOriginal
2023-05-17 11:01:07635browse

With the advent of the Internet and digital age, files are used more and more frequently. Therefore, the management of files becomes increasingly important. For some useless or expired files, we need to clean them regularly to free up disk space and improve system efficiency. In Nodejs, we can easily use scheduled tasks to delete files.

1. Overview of scheduled tasks

In Nodejs, we can create scheduled tasks by using the third-party library node-schedule. node-schedule is a library that allows you to easily schedule scheduled tasks. It provides the function of executing tasks at a specified time or interval.

2. Use node-schedule to delete files

First we need to install the node-schedule library. We can execute the following command in the terminal:

npm install node-schedule

Next we can create a node.js file, such as delete_files.js. In this file, we can:

  1. Introduce the node-schedule library:
const schedule = require('node-schedule');
  1. Create a function to delete files:
function deleteFile(filePath) {
   fs.unlink(filePath, (err) => {
       if (err) throw err;
       console.log(`${filePath}删除成功`);
   });
}
  1. Call our delete function in the scheduled task:
const job = schedule.scheduleJob('0 0 * * *', function() { 
   // 在每天的0点执行删除任务
   const filePath = './path/to/file'; // 文件路径
   deleteFile(filePath); 
});

Among them, the first parameter of the scheduleJob function is a string A cron expression of type consisting of five space-separated time units, representing minutes, hours, dates, months, and days of the week.

  1. Finally, we can enter the following command on the command line to run our scheduled deletion task:
node delete_files.js

3. Delete all files in the folder

If we need to delete all files in a folder, we can also do it by creating a function. As shown below:

function deleteFolderFiles(folderPath) {
   fs.readdir(folderPath, (err, files) => {
       if (err) throw err;

       for (const file of files) {
           const filePath = path.join(folderPath, file);
           fs.unlink(filePath, (err) => {
               if (err) throw err;
               console.log(`${filePath}删除成功`);
           });
       }
   });
}

In the scheduled task, we can use this function to perform the deletion operation:

const job = schedule.scheduleJob('0 0 * * *', function() { 
   // 在每天的0点执行删除任务
   const folderPath = './path/to/folder'; // 文件夹路径
   deleteFolderFiles(folderPath); 
});

4. Complete code implementation

The following is the file and Example of integrating the code to delete all files in a folder:

const schedule = require('node-schedule');
const fs = require('fs');
const path = require('path');

// 删除单个文件
function deleteFile(filePath) {
   fs.unlink(filePath, (err) => {
       if (err) throw err;
       console.log(`${filePath}删除成功`);
   });
}

// 删除文件夹内所有文件
function deleteFolderFiles(folderPath) {
   fs.readdir(folderPath, (err, files) => {
       if (err) throw err;

       for (const file of files) {
           const filePath = path.join(folderPath, file);
           fs.unlink(filePath, (err) => {
               if (err) throw err;
               console.log(`${filePath}删除成功`);
           });
       }
   });
}

// 删除单个文件定时任务
const job1 = schedule.scheduleJob('0 0 * * *', function() { 
   // 在每天的0点执行删除任务
   const filePath = './path/to/file'; // 文件路径
   deleteFile(filePath); 
});

// 删除文件夹内所有文件定时任务
const job2 = schedule.scheduleJob('0 0 * * *', function() { 
   // 在每天的0点执行删除任务
   const folderPath = './path/to/folder'; // 文件夹路径
   deleteFolderFiles(folderPath); 
});

Through scheduled tasks, we can easily delete some useless or expired files, free up disk space, and improve system efficiency. At the same time, we can also create different scheduled tasks according to different needs to make our system management more refined.

The above is the detailed content of nodejs+ file regularly deletes files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:nodejs ghost change portNext article:nodejs ghost change port