Nodejs implementation traverses the folder and counts the file size _node.js

Source: Internet
Author: User
Tags readfile

I have been away from the company for more than 2 months and have written a lot of tools before, but on the day I left the company I personally formatted all the accumulated tools and code for the year. Today I think of a problem that I have encountered in the project and record it today.

I am in the optimization of memory, encountered some pictures in the load when it is difficult to loading, found some skills icon, small size 50x50, but the picture size is very large, there are several m, so find the path, found to be the art output icon because of the forgotten compression caused the icon become very large, Therefore, the loading occurs constantly loading phenomenon appears.

There are as many as thousands of art documents in the project, a piece to find a problem is really a physical work, then use the free time to try the Nodejs file system API piece wrote a small demo, meet the needs, quickly find the problem, the picture of the problem to tell the art to change over. Solve the problem like this.

About this file statistics size is very practical, the front can be counted on these pictures have targeted larger pictures to compress. Because there is a data for reference can be very convenient to find a problem.

The following use Nodejs to traverse the contents of the folder file, and read all the files, and take the order to the large to small output, and finally generate a file, the file has been sorted. You can see if those files have files.

var fs = require (' FS ')//Traverse folder, get file information in all folders/* @param path PATH */function Gefilelist (path) {var fileslist =
 [];
 ReadFile (path,fileslist);
return fileslist;
 }//Traversal read file function ReadFile (path,fileslist) {files = Fs.readdirsync (path);//need to use synchronous read Files.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;//filename Obj.path = path+ '/' +file;
  File absolute path Fileslist.push (obj);
 Write file Utf-8 Format function WriteFile (filename,data) {fs.writefile (filename,data, ' utf-8 ', complete);
 function complete () {console.log ("file build succeeded");
} var fileslist = gefilelist ("G:/nodejs");
Fileslist.sort (Sorthandler);
 function Sorthandler (a,b) {if (A.size > B.size) return-1;
else if (A.size < b.size) return 1 return 0;
} var str= '; for (Var I=0;i<fileslist.length;i++) {var item = Fileslist[i];
 var desc = "FileName:" +item.name + "+" Size: "+ (item.size/1024). toFixed (2) +"/kb "+" "+" path: "+item.path;

 Str+=desc + "\ n"} writefile ("Test.txt", str);


The use of the method is simple: the var fileslist = gefilelist ("G:/nodejs"); Modify the Getfilelist parameters to the path you want, change the parameters in the path, that is, you can traverse the folder file, and generate a file.

When there are many files, it is recommended to use a powerful text editor so that it is easy to read.

Below is the size of the file

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.