使用批处理文件获取以“MB"为单位的目录大小 [英] Get size of a directory in 'MB' using batch file
本文介绍了使用批处理文件获取以“MB"为单位的目录大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用批处理文件在 MB
中获取目录的大小,例如 C:Temp
.我不需要子目录或文件的大小,而是目录本身的大小.
I want to get the size of directory say C:Temp
in MB
using batch file. I don't need sizes of child directories or files, but the size of directory itself.
但它给了我字节的大小和子文件夹的大小.所以我的问题是:
but it gives me size in bytes and that of sub folders. So my question is :
如何以MB为单位获取目录本身的大小?
How to get the size of directory itself in MB ?
推荐答案
您可以使用混合脚本 [Batch/Vbscript] 来做到这一点:
You can do it with a hybrid script [Batch/Vbscript] like this one :
@echo off
set Folder="C: emp"
echo The size of %Folder% is
Call :GetSize %Folder%
pause
:GetSize
(
echo wscript.echo GetSize("%~1"^)
echo Function GetSize(MyFolder^)
echo Set fso = CreateObject("Scripting.FileSystemObject"^)
echo Set objFolder= fso.GetFolder(MyFolder^)
echo GetSize = FormatSize(objFolder.Size^)
echo End Function
echo '*******************************************************************
echo 'Function to format a number into typical size scales
echo Function FormatSize(iSize^)
echo aLabel = Array("bytes", "KB", "MB", "GB", "TB"^)
echo For i = 0 to 4
echo If iSize ^> 1024 Then
echo iSize = iSize / 1024
echo Else
echo Exit For
echo End If
echo Next
echo FormatSize = Round(iSize,2^) ^& " " ^& aLabel(i^)
echo End Function
echo '*******************************************************************
)>%tmp%Size.vbs
Cscript /NoLogo %tmp%Size.vbs
Del %tmp%Size.vbs
Exit /b
2016 年 3 月 30 日 @12:11
这是一个很好的技巧
用于批量嵌入 vbscode 的 Liviu hack,无需临时文件
the Liviu's hack for embedding vbscode in batch without temp files
我刚刚从 npocmaka 发现,感谢他
I just discovered from npocmaka thanks to him
@echo off
Set Folder="c: emp"
@cScript.EXE //noLogo "%~f0?.WSF" %Folder% //job:info %~nx0%*
pause
@exit /b 0
<job id="info">
<script language="VBScript">
wscript.echo GetSize(WScript.Arguments(0))
Function GetSize(MyFolder)
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder= fso.GetFolder(MyFolder)
GetSize = FormatSize(objFolder.Size)
End Function
'*******************************************************************
'Function to format a number into typical size scales
Function FormatSize(iSize)
aLabel = Array("bytes", "KB", "MB", "GB", "TB")
For i = 0 to 4
If iSize > 1024 Then
iSize = iSize / 1024
Else
Exit For
End If
Next
FormatSize = Round(iSize,2) & " " & aLabel(i)
End Function
'*******************************************************************
</script>
</job>
这篇关于使用批处理文件获取以“MB"为单位的目录大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文