批处理获取H盘已用空间
发布网友
发布时间:2023-09-28 20:54
我来回答
共2个回答
热心网友
时间:2023-10-17 04:49
@echo off&title H盘容量使用情况 By 依梦琴瑶
>DiskCalc1.vbs echo WSCript.Echo "盘符" ^& Chr(9) ^& "总容量(GB)" ^& Chr(9) ^& "已用空间(GB)" ^& Chr(9) ^& "剩余空间(GB)" ^& Chr(9) ^& "使用率"
>DiskCalc2.vbs echo DeviceID = Wscript.Arguments(0)
>>DiskCalc2.vbs echo SizeAvail = Wscript.Arguments(1) / (1024 * 1024 * 1024)
>>DiskCalc2.vbs echo SizeTotal = Wscript.Arguments(2) / (1024 * 1024 * 1024)
>>DiskCalc2.vbs echo SizeUsed = SizeTotal - SizeAvail
>>DiskCalc2.vbs echo SizeRate = SizeUsed / SizeTotal * 100
>>DiskCalc2.vbs echo WSCript.Echo DeviceID ^& Chr(9) ^& Round(SizeTotal,1) ^& Chr(9) ^& Chr(9) ^& Round(SizeUsed,1) ^& Chr(9) ^& Chr(9) ^& _
>>DiskCalc2.vbs echo Round(SizeAvail,1) ^& Chr(9) ^& Chr(9) ^& Round(SizeRate,1) ^& "%%"
cscript //nologo DiskCalc1.vbs
(for /f "tokens=1-3" %%a in ('wmic LogicalDisk where "DeviceID='H:'" get DeviceID ^, Size ^, FreeSpace ^| findstr ":"') do (
cscript //nologo DiskCalc2.vbs %%a %%b %%c
))
del DiskCalc*.vbs
echo,&pause
以下为我电脑所有本地硬盘的测试效果,上面代码已根据你的要求修改。
热心网友
时间:2023-10-17 04:50
不清楚你的实际文件/情况,仅以问题中的说明及猜测为据;以下代码复制粘贴到记事本,另存为xx.bat,编码选ANSI/*&cls&echo off&cd /d "%~dp0"
rem 获取指定磁盘分区的总容量、已用容量和剩余容量
set #=Any question&set _=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%_% %z%
rem 设置需查询空间情况的盘符,多个盘符请以空格相隔,如C D E
set "partition=H"
wmic LOGICALDISK get DeviceID,FreeSpace,Size|cscript -nologo -e:jscript "%~f0" "%partition::=%"
pause
exit
*/
function fs(z){
var t='';
if(z >= Math.pow(1024,4)){
t=(z/Math.pow(1024,4)).toFixed(2).replace(/^\./,'0.')+'TB';
}else{
t=(z/Math.pow(1024,3)).toFixed(2).replace(/^\./,'0.')+'GB';
}
return t;
}
var drive=WSH.Arguments(0).split(/\s+/),dic={};
while(!WScript.StdIn.AtEndOfStream){
var line=WScript.StdIn.ReadLine().replace(/^\s*|\s*$/g,'');
if(line.indexOf(':') > -1){
var arr=line.split(/\s+/),p=arr[0].toUpperCase().replace(':','');
if(dic[p] == undefined){
var TotalSpace=Number(arr[2]);
var FreeSpace=Number(arr[1]);
var UsedSpace=TotalSpace-FreeSpace;
var UsedRate=(UsedSpace/TotalSpace*100).toFixed(2)+'%';
dic[p]=[arr[0],fs(TotalSpace),fs(FreeSpace),fs(UsedSpace),UsedRate];
}
}
}
WSH.StdErr.WriteLine('Drive, TotalSpace, FreeSpace, UsedSpace, UsedRate');
for(var i=0;i<drive.length;i++){
var p=drive[i].toUpperCase().replace(':','');
if(dic[p] != undefined){
WSH.StdErr.WriteLine(dic[p].join(', '));
}
}
WSH.Quit();