Nodejs traverses the folder and counts the file size.

Source: Internet
Author: User

Nodejs traverses the folder and counts the file size.

I have been away from the company for more than two months. I wrote a lot of 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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

Var fs = require ('fs ')

 

 

// Traverse the folder to obtain the file information in all folders

/*

* @ Param path

*

*/

 

Function geFileList (path)

{

Var filesList = [];

ReadFile (path, filesList );

Return filesList;

}

 

// Traverse and read files

Function readFile (path, filesList)

{

Files = fs. readdirSync (path); // synchronous reading is required.

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; // file name

Obj. path = path + '/' + file; // absolute file path

FilesList. push (obj );

}

}

}

 

// Write the file in UTF-8 format

Function writeFile (fileName, data)

{

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 (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 = "file name:" + item. name + ""

+ "Size:" + (item. size/1024). toFixed (2) + "/kb" + ""

+ "Path:" + item. path;

Str + = desc + "\ n"

}

 

 

WriteFile ("test.txt", str );

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.