如何在Windows系统中使用JavaScript计算磁盘可用空间及占比?
Windows系统下JavaScript获取磁盘可用空间及占比方案
浏览器端JavaScript受安全限制无法直接访问系统磁盘信息,因此需要基于Node.js环境实现以下方案:
方案一:使用Node.js原生模块调用Windows系统命令
通过child_process模块执行Windows内置的wmic命令获取磁盘信息,无需额外依赖:
const { execSync } = require('child_process'); try { // 执行wmic命令获取磁盘信息,输出为CSV格式便于解析 const output = execSync('wmic logicaldisk get size,freespace,caption /format:csv', { encoding: 'utf8' }); // 解析CSV输出 const lines = output.trim().split('\n'); const headers = lines[0].split(',').map(item => item.trim()); const diskList = lines.slice(1) .map(line => { const values = line.split(',').map(item => item.trim()); const diskInfo = {}; headers.forEach((header, idx) => diskInfo[header] = values[idx]); return diskInfo; }) .filter(disk => disk.Size && disk.FreeSpace); // 过滤无有效空间信息的磁盘(如光驱) // 计算并输出每个磁盘的空间占比 diskList.forEach(disk => { const totalBytes = parseInt(disk.Size); const freeBytes = parseInt(disk.FreeSpace); const usedBytes = totalBytes - freeBytes; const freeRatio = ((freeBytes / totalBytes) * 100).toFixed(2); const usedRatio = ((usedBytes / totalBytes) * 100).toFixed(2); console.log(`磁盘 ${disk.Caption}:`); console.log(` 总空间: ${(totalBytes / 1024 / 1024 / 1024).toFixed(2)} GB`); console.log(` 可用空间: ${(freeBytes / 1024 / 1024 / 1024).toFixed(2)} GB (${freeRatio}%)`); console.log(` 已用空间: ${(usedBytes / 1024 / 1024 / 1024).toFixed(2)} GB (${usedRatio}%)`); console.log('---'); }); } catch (err) { console.error('获取磁盘信息失败:', err.message); }
方案二:使用第三方npm包简化开发
如果不想自己解析系统命令输出,可以使用diskspace包,该包封装了跨平台的磁盘空间获取逻辑:
- 先安装依赖:
npm install diskspace
- 示例代码:
const diskspace = require('diskspace'); // 获取单个磁盘信息 diskspace.check('C:', (err, result) => { if (err) { console.error('获取磁盘信息失败:', err); return; } const totalGB = (result.total / 1024 / 1024 / 1024).toFixed(2); const freeGB = (result.free / 1024 / 1024 / 1024).toFixed(2); const usedGB = ((result.total - result.free) / 1024 / 1024 / 1024).toFixed(2); const freeRatio = ((result.free / result.total) * 100).toFixed(2); const usedRatio = (((result.total - result.free) / result.total) * 100).toFixed(2); console.log(`磁盘 C::`); console.log(` 总空间: ${totalGB} GB`); console.log(` 可用空间: ${freeGB} GB (${freeRatio}%)`); console.log(` 已用空间: ${usedGB} GB (${usedRatio}%)`); }); // 获取所有磁盘信息 diskspace.drives((err, drives) => { if (err) { console.error('获取磁盘列表失败:', err); return; } drives.forEach(drive => { diskspace.check(drive, (err, result) => { if (err || !result.total) return; // 跳过无法获取信息的磁盘 const totalGB = (result.total / 1024 / 1024 / 1024).toFixed(2); const freeGB = (result.free / 1024 / 1024 / 1024).toFixed(2); const usedGB = ((result.total - result.free) / 1024 / 1024 / 1024).toFixed(2); const freeRatio = ((result.free / result.total) * 100).toFixed(2); const usedRatio = (((result.total - result.free) / result.total) * 100).toFixed(2); console.log(`磁盘 ${drive}:`); console.log(` 总空间: ${totalGB} GB`); console.log(` 可用空间: ${freeGB} GB (${freeRatio}%)`); console.log(` 已用空间: ${usedGB} GB (${usedRatio}%)`); console.log('---'); }); }); });
注意事项
- 以上方案均需运行在Node.js环境中,浏览器端无法直接实现该需求,若为前端项目需通过Node.js后端接口获取数据。
- 使用
wmic命令时需确保当前用户有足够权限访问磁盘信息。
内容的提问来源于stack exchange,提问作者Kenan Dadashzade
相关产品推荐
相关产品推荐

