如何獲取目錄中文件的總計大小?最好的辦法 ?如何獲取目錄中文件的總計大小?
回答
這是一個簡單的解決方案,它使用核心Nodejs fs庫與異步庫結合使用。它完全異步,應該像'du'命令一樣工作。
var fs = require('fs'),
path = require('path'),
async = require('async');
function readSizeRecursive(item, cb) {
fs.lstat(item, function(err, stats) {
if (!err && stats.isDirectory()) {
var total = stats.size;
fs.readdir(item, function(err, list) {
if (err) return cb(err);
async.forEach(
list,
function(diritem, callback) {
readSizeRecursive(path.join(item, diritem), function(err, size) {
total += size;
callback(err);
});
},
function(err) {
cb(err, total);
}
);
});
}
else {
cb(err);
}
});
}
查看node.js File System functions。看起來您可以使用fs.readdir(path, [cb])
和fs.stat(file, [cb])
的組合來列出目錄中的文件並對它們的大小進行求和。
像這樣(沒有經過測試):
var fs = require('fs');
fs.readdir('/path/to/dir', function(err, files) {
var i, totalSizeBytes=0;
if (err) throw err;
for (i=0; i<files.length; i++) {
fs.stat(files[i], function(err, stats) {
if (err) { throw err; }
if (stats.isFile()) { totalSizeBytes += stats.size; }
});
}
});
// Figure out how to wait for all callbacks to complete
// e.g. by using a countdown latch, and yield total size
// via a callback.
請注意,此解決方案只考慮直接存儲在目標目錄中的純文本文件,並執行沒有遞歸。通過檢查stats.isDirectory()
並進入,自然會產生遞歸解決方案,儘管它可能使「等待完成」步驟複雜化。
這種解決方案需要包含在fs.stat調用的相對路徑,否則你會得到ENOENT錯誤。 – citizenslave
'找出如何等待所有回調完成'最簡單的方法是使用基於Promise的庫並使用'Promise.all()' – samvv
我測試了下面的代碼,它工作得很好。 請讓我知道,如果有什麼你不明白的。
var util = require('util'),
spawn = require('child_process').spawn,
size = spawn('du', ['-sh', '/path/to/dir']);
size.stdout.on('data', function (data) {
console.log('size: ' + data);
});
// --- Everything below is optional ---
size.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
size.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
第二個方法:
您可能要參考Node.js的API爲child_process
Windows操作系統是一件... – iOnline247
使用DU:https://www.npmjs.org/package/du
require('du')('/home/rvagg/.npm/', function (err, size) {
console.log('The size of /home/rvagg/.npm/ is:', size, 'bytes')
})
ES6變種:
import path_module from 'path'
import fs from 'fs'
// computes a size of a filesystem folder (or a file)
export function fs_size(path, callback)
{
fs.lstat(path, function(error, stats)
{
if (error)
{
return callback(error)
}
if (!stats.isDirectory())
{
return callback(undefined, stats.size)
}
let total = stats.size
fs.readdir(path, function(error, names)
{
if (error)
{
return callback(error)
}
let left = names.length
if (left === 0)
{
return callback(undefined, total)
}
function done(size)
{
total += size
left--
if (left === 0)
{
callback(undefined, total)
}
}
for (let name of names)
{
fs_size(path_module.join(path, name), function(error, size)
{
if (error)
{
return callback(error)
}
done(size)
})
}
})
})
}
- 1. 如何統計目錄中文件的總體大小
- 2. 獲取目錄中文件的大小
- 3. 總結目錄中的文件大小
- 4. 的iOS - 獲取文件大小的總和目錄
- 5. 如何獲取C中子目錄的總大小?
- 6. PowerShell腳本獲取目錄總大小
- 7. 如何在獲取所有文件大小的相同函數中獲得總目錄大小? (Python)
- 8. 如何在C#中獲取目錄大小(目錄中的文件)?
- 9. 如何在目錄下遞歸獲取所有文件的總大小
- 10. Laravel - 計算目錄的總大小?
- 11. 如何在Linux中列出目錄中文件和文件夾的總大小?
- 12. PHPSECLIB讀取目錄的總大小?
- 13. 獲取目錄大小
- 14. 如何獲取目錄大小,HTML5文件API,使用webkit目錄
- 15. 如何獲取包含子目錄的目錄中的特定文件總數?
- 16. 獲取目錄和子目錄中的所有.gz文件的大小 - python
- 17. C++如何獲取PNG文件的圖像大小(在目錄中)
- 18. 在批處理文件中使用Powershell獲取當前目錄的總大小和計數時出錯
- 19. 從目錄參數中獲取文件,按大小排序
- 20. MSSQL:獲取總計小計
- 21. 如何獲取WAR文件的大小?
- 22. 如何獲取C文件的大小?
- 23. 如何獲取文件的大小
- 24. 如何獲取文件夾的大小?
- 25. C++:獲取的所有文件的大小當前目錄
- 26. 從目錄和子目錄中獲取所有文件,大小,路徑php
- 27. 如何獲取小時總計
- 28. OSX:獲取磁盤上文件/目錄的大小
- 29. 使用Java 7獲取文件/目錄大小新的IO
- 30. PHP目錄讀取,計數和文件大小
'path.join(item, diritem)'是正確的?當我啓動函數時,它返回'TypeError:不能調用未定義的方法'join' – inetbug
你加載了'path'模塊嗎? – loganfsmyth
不,我認爲更好地清楚地顯示代碼中加載所有必要的模塊。 – inetbug