You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中用Glob模式排除路径获取文件夹大小及预计算归档大小

Calculating Directory Sizes with Glob Exclusions in Node.js (Plus Progress Bar for Archiving)

Got it, let's break down how to solve both of your Node.js requirements clearly. We'll use established packages to keep things clean and reliable.

1. Calculate Target Directory Size with Glob Exclusions

First, we'll need two packages: glob to handle pattern matching/exclusions, and filesize to format the final size into something human-readable. Node.js's built-in fs.promises will handle file stats.

Step 1: Install Dependencies

npm install glob filesize

Step 2: Implementation Code

const glob = require('glob').promises;
const fs = require('fs').promises;
const filesize = require('filesize');

async function getDirectorySize(targetDir, excludePatterns = []) {
  try {
    // Match all files in the target dir, ignore specified glob patterns
    const filePaths = await glob(`${targetDir}/**/*`, {
      ignore: excludePatterns,
      nodir: true, // Only return files, not directories
      absolute: true // Get full file paths
    });

    let totalSize = 0;

    // Iterate through each file and accumulate size
    for (const filePath of filePaths) {
      const stats = await fs.stat(filePath);
      // Skip symlinks if needed (uncomment below)
      // if (stats.isSymbolicLink()) continue;
      totalSize += stats.size;
    }

    return {
      bytes: totalSize,
      humanReadable: filesize(totalSize)
    };
  } catch (err) {
    console.error('Error calculating directory size:', err);
    throw err;
  }
}

// Example usage
(async () => {
  const size = await getDirectorySize('./my-project', [
    '**/node_modules/**',
    '**/*.log',
    '**/.git/**'
  ]);
  console.log(`Total size: ${size.humanReadable} (${size.bytes} bytes)`);
})();

Key Notes:

  • The ignore option takes an array of glob patterns to exclude (e.g., node_modules, log files)
  • nodir: true ensures we only process files (directories themselves don't contribute to size)
  • We use fs.stat to get each file's size, and skip symlinks if you uncomment that line

2. Calculate Total Size for Archiving with Progress Bar & Glob Filters

For this task, we'll add a progress bar using cli-progress to give real-time feedback while calculating the total size. This is helpful for large directories where the calculation might take time.

Step 1: Install Additional Dependency

npm install cli-progress

Step 2: Implementation Code

const glob = require('glob').promises;
const fs = require('fs').promises;
const filesize = require('filesize');
const cliProgress = require('cli-progress');

async function calculateArchiveSize(targetDir, excludePatterns = []) {
  try {
    // First, get all files we need to process
    const filePaths = await glob(`${targetDir}/**/*`, {
      ignore: excludePatterns,
      nodir: true,
      absolute: true
    });

    const totalFiles = filePaths.length;
    let totalSize = 0;

    // Initialize progress bar
    const progressBar = new cliProgress.SingleBar({
      format: 'Calculating size | {bar} | {percentage}% | {value}/{total} files | ETA: {eta}s',
      barCompleteChar: '\u2588',
      barIncompleteChar: '\u2591',
      hideCursor: true
    });
    progressBar.start(totalFiles, 0);

    // Process each file, update progress bar and total size
    for (let i = 0; i < filePaths.length; i++) {
      const filePath = filePaths[i];
      const stats = await fs.stat(filePath);
      // Skip symlinks if needed
      // if (stats.isSymbolicLink()) continue;
      totalSize += stats.size;
      progressBar.update(i + 1);
    }

    progressBar.stop();
    return {
      bytes: totalSize,
      humanReadable: filesize(totalSize),
      totalFilesProcessed: totalFiles
    };
  } catch (err) {
    console.error('Error calculating archive size:', err);
    throw err;
  }
}

// Example usage (call before starting your archiving process)
(async () => {
  const archiveStats = await calculateArchiveSize('./my-project', [
    '**/node_modules/**',
    '**/dist/**',
    '**/*.tmp'
  ]);
  console.log(`\nReady to archive: ${archiveStats.humanReadable} (${archiveStats.totalFilesProcessed} files)`);
  // Now start your archiving logic (e.g., using tar/zip packages)
})();

Key Notes:

  • We first fetch all file paths upfront so we know the total number of files for the progress bar
  • The progress bar updates after each file is processed, showing percentage, files completed, and ETA
  • Once the calculation is done, you can pass the total size to your archiving tool (like tar or adm-zip) to show progress during the actual archiving step

内容的提问来源于stack exchange,提问作者Kombye

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 12:22:30