NodeJS 查找一个空文件夹,但它查找列表中的最后一个文件夹

fkaflof6  于 2022-12-18  发布在  Node.js
关注(0)|答案(1)|浏览(170)

有一个列表spisok列表中的每个项目都是某个文件夹的路径。
我们需要找到第一个空的(小于1 mb)文件夹,并将其路径写入itog变量,但必须将其放置在spisoklist中,后面跟着大小为100mb或更大的文件夹。
已尝试Nodejs 18软件包获取文件夹大小和getFolderSize函数,但不起作用。
使用Nodejs 18。使用内置文件系统工具实现文件夹大小确定。但在变量itog中,我只从列表中获得最后一个文件夹spisok,它的重量为41 mb...不符合小于1 mb的条件,并且列表中它的后面应该是超过100 mb的文件夹这也不起作用,因为在它之后列表中没有项目spisok

代码:

// Step 1
const fs = require('fs');
const path = require('path');

const spisok = spisok;

// Declare variables
let pytkpystoypapke = ""; // path to found folder with size less than 1000000 bytes
let itog = ""; // path to found folder will be saved in this variable

// Loop through all folders in list "spisok"
for (let i = 0; i < spisok.length; i++) {
  let folderSize = fs.statSync(spisok[i]).size; // get size of current folder
  if (folderSize < 1000000) { // check if size is less than 1000000 bytes
    if (i < spisok.length - 1) { // check if there is another folder in list "spisok"
      let nextFolderSize = fs.statSync(spisok[i + 1]).size; // get size of next folder
      if (nextFolderSize > 1000000) { // check if next folder size is greater than 1000000 bytes
        pytkpystoypapke = spisok[i]; // save path to current folder in "pytkpystoypapke"
        break; // exit the loop
      }
    } else {
      pytkpystoypapke = spisok[i]; // save path to current folder in "pytkpystoypapke"
      break; // exit the loop
    }
  }
}

itog = pytkpystoypapke; // save path to found folder in "itog" variable

// Output result
console.log(itog);
8e2ybdfx

8e2ybdfx1#

我自己找到了解决方案。下面是完成的代码,也许对某人有用:

// Declare the list "spisok"
let spisok = [[SPISOK]];
const fs = require('fs');

// Use a for loop to go through all elements of the list "spisok"
for (let i = 0; i < spisok.length; i++) {

    let folderPath = spisok[i]; // Assign each value of the loop to the variable "folderPath"

    //console.log(folderPath); // Print the value of "folderPath" in the log


    // шаг 1. начитаем получать размер папки путь к ней лежит в folderPath
    function getFolderSize(path) {
    let totalSize = 0;
    const files = fs.readdirSync(path);
    for (let i = 0; i < files.length; i++) {
    const stats = fs.statSync(path + '//' + files[i]);
    if (stats.isFile()) {
    totalSize += stats.size;
    }
    else if (stats.isDirectory()) {
    totalSize += getFolderSize(path + '//' + files[i]);
    }
    }
    return totalSize;
    }
    const folderSize = getFolderSize(folderPath);
    // закончили получать размер папки "folderPath" 
    // и размер записали в "folderSize"



    if (folderSize < 1000000); {

        // шаг 2. получаем размер папки лежащей ниже
            let nextElement = spisok[i + 1];
            function getFolderSize(path) {
            let totalSize = 0;
            const files = fs.readdirSync(path);
            for (let i = 0; i < files.length; i++) {
            const stats = fs.statSync(path + '//' + files[i]);
            if (stats.isFile()) {
            totalSize += stats.size;
            }
            else if (stats.isDirectory()) {
            totalSize += getFolderSize(path + '//' + files[i]);
            }
            }
            return totalSize;
            }
            const nextFolderSize = getFolderSize(nextElement);
  
        // закончили получать размер папки лежащей ниже и запись
        // записали размер в "nextFolderSize"

        if (nextFolderSize > 100000000) {
             console.log(folderPath);
             //console.log(folderSize);
             //console.log(folderPath);
             [[PYT_K_PROSHLOY_PYSTOY_PAPKE]] = folderPath;
             break
}
}
}

相关问题