Nodejs traverses the folder and counts the file size _ node. js

Source: Internet
Author: User
This article mainly introduces nodejs to traverse folders and measure the file size. The following uses nodejs to traverse folder files and read all files, and output the data in ascending to smallest order. If you need a friend, you can refer to the fact that you have been away from the company for more than two months. You have written many tools before, but on the day I left the company, I personally formatted all the accumulated tools and code over the past year. Today I think of a problem I encountered in my project. I will record it today.

When I was optimizing the memory, I encountered some images that were difficult to loading during loading. I found some skill icons with a size of 50x50, but the image size was large, several MB, so I found the path and found that when I forgot to compress the output icon, the icon became very large, so the loading was not interrupted.

There are as many as thousands of art files in the project. It is really a kind of physical work to find the problem one by one. At that time, I tried to piece together the API of the nodejs File System to write a small demo, after meeting the requirements, I quickly found the problem and told art to modify the problematic image. This solves the problem.

Statistics on the size of this file is very practical. The front-end can calculate the size of these images for compression. Because there is a data for reference, you can easily find the problem.

The following uses nodejs to traverse the content of the folder file, read all the files, output the files in ascending order of order, and finally generate a file, which has been sorted. Check whether the files have files.

Var fs = require ('fs') // traverse the folder to obtain the file information in all the folders/** @ param path **/function geFileList (path) {var filesList = []; readFile (path, filesList); return filesList;} // traverses and reads the file function readFile (path, filesList) {files = fs. readdirSync (path); // synchronous reading of files is required. forEach (walk); function walk (file) {states = fs. statSync (path + '/' + file); if (states. isDirectory () {readFile (path + '/' + file, filesList);} else {// create an Object to save information var obj = new Object (); obj. size = states. size; // file size, in bytes. obj. name = file; // file name obj. path = path + '/' + file; // file absolute path filesList. push (obj) ;}}// write the function writeFile (fileName, data) in UTF-8 format to the file {fs. writeFile (fileName, data, 'utf-8', complete); function complete () {console. log ("file generated successfully") ;}} var filesList = geFileList ("G:/nodejs"); filesList. sort (sortHandler); function sortHandler (a, B) {if (. size> B. size) return-1; else if (. size <B. size) return 1 return 0;} var str = ''; for (var I = 0; I
 
  


The method is simple: Change var filesList = geFileList ("G:/nodejs"); change the getFileList parameter to the desired path, and change the parameter path in it, that is, you can traverse the folder and generate a file.

When the number of files is large, we recommend that you use a powerful text editor to facilitate reading.

Below is the file size

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.