Count the number of files in a directory using JavaScript/nodejs?

前端 未结 7 2315
小蘑菇
小蘑菇 2021-02-12 17:54

How can I count the number of files in a directory using nodejs with just plain JavaScript or packages? I want to do something like this:

How to count the n

7条回答
  •  误落风尘
    2021-02-12 18:52

    1) Download shell.js and node.js (if you don't have it)
    2) Go where you download it and create there a file named countFiles.js

    var sh = require('shelljs');
    
    var count = 0;
    function annotateFolder (folderPath) {
      sh.cd(folderPath);
      var files = sh.ls() || [];
    
      for (var i=0; i

    3) Open the command promt in the shelljs folder (where countFiles.js is) and write node countFiles "DESTINATION_FOLDER" (e.g. node countFiles "C:\Users\MyUser\Desktop\testFolder")

提交回复
热议问题