nodejs delete specified file size

PHPz
Release: 2023-05-08 17:50:37
Original
461 people have browsed it

Node.js is a popular back-end JavaScript running environment. With various modules and packages, it can complete many common tasks. Among them, file system processing is one of the essential functions in Node.js. In file system operations, deleting a specified file size is a common requirement. This article will introduce in detail how to delete a specified file size using Node.js.

1. Node.js file system

The file system (fs) module of Node.js provides a series of methods that allow us to easily perform file system operations, such as creating and reading. , write, delete, etc. To use the fs module, we need to introduce it first:

const fs = require('fs');
Copy after login

Among them, the commonly used methods in the fs module are the following:

  1. fs.unlink(path, callback): delete Files in the specified path.
  2. fs.readdir(path, callback): Read all files and subdirectories in a directory.
  3. fs.stat(path, callback): Get the attributes of a file or directory.
  4. fs.rename(oldPath, newPath, callback): Rename or move the file.
  5. fs.mkdir(path, callback): Create a directory.
  6. fs.rmdir(path, callback): Delete a directory.

2. Delete the specified file size

Deleting the specified file size is a very common need, especially when we need to clean up unnecessary large files. In Node.js, you can use the stat method of the fs module to get the size of the file, and then filter and delete it based on the size.

First, we first define the directory path and file size threshold of the files that need to be deleted:

const path = './path/to/files'; // 文件目录 const sizeThreshold = 1048576; // 文件大小的阈值(1MB)
Copy after login

Then, we use fs.readdir to read all the files in the directory, and then filter and select Remove the files that need to be deleted. In this process, we use the Promise.all() method to wait for the calculation of the size values of all files so that we can delete the files later.

fs.readdir(path, (err, files) => { if (err) { throw err; } const promises = []; files.forEach(file => { const filePath = `${path}/${file}`; const statPromise = new Promise((resolve, reject) => { fs.stat(filePath, (err, stats) => { if (err) { reject(err); } else { resolve(stats.size); } }); }); promises.push(statPromise); }); Promise.all(promises).then(sizes => { files.forEach((file, index) => { const filePath = `${path}/${file}`; const size = sizes[index]; if (size >= sizeThreshold) { fs.unlink(filePath, err => { if (err) { console.error(`Failed to delete file: ${filePath}`); } else { console.log(`File deleted: ${filePath}`); } }); } }); }); });
Copy after login

In the above code, we first calculate the sizes of all files in the directory and store the size values in asizesarray. Then iterate through all files, and if the file size exceeds the threshold, call the fs.unlink method to delete the file.

When deleting files, we use the asynchronous method based on Promise instead of the callback function method of fs.unlink(). This approach can not only improve the simplicity of the code, but also effectively avoid the callback hell problem.

3. Summary

In Node.js, the fs module can be used to conveniently perform file system operations. Delete specified file size is a very useful function that allows us to easily clean up unnecessary large files. Through the introduction of this article, we learned how to use Node.js to delete a specified file size. We also introduced how to use Promise. We hope it can help everyone.

The above is the detailed content of nodejs delete specified file size. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!