标题: [转贴] VBS计算文件、文件夹大小 [打印本页]
作者: qq692172929 时间: 2011-7-17 00:16 标题: VBS计算文件、文件夹大小
- set open=createobject("scripting.filesystemobject")
- str=inputbox("请输入文件(夹)名称或文件(夹)完整路径:",wscript.scriptname)
- if open.fileexists(str) then
- f1=open.getfile(str).size
- if f1<=1024 then
- sum=f1/1024
- t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
- else
- if f1<=1024^2 then
- sum=f1/1024^2
- t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
- else
- sum=f1/1024^2
- t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
- end if
- end if
- else
- f2=open.getfolder(str).size
- if f2<=1024 then
- sum=f2/1024
- t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
- else
- if f2<=1024^2 then
- sum=f2/1024^2
- t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
- else
- sum=f2/1024^2
- t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
- end if
- end if
- end if
- msgbox round(t,2)&" MB",64,wscript.scriptname
复制代码
作者: powerbat 时间: 2011-7-17 10:22
- function HumanSize(size)
- Dim Units, Gates, SizeStr
- Units = Array("Byte", "KB", "MB", "GB", "TB")
- Gates = Array(1, 1024, 1024^2, 1024^3, 1024^4)
-
- SizeStr = "0Byte"
- for i=UBound(Units) to 0 Step -1
- if size>=Gates(i) then
- SizeStr = FormatNumber(Size/Gates(i), 2) & Units(i)
- exit for
- end if
- next
- HumanSize = SizeStr
- end function
-
- 'example
- set fso = CreateObject("Scripting.FileSystemObject")
- Size = fso.GetFolder(".").Size
- WScript.Echo Size & " = " & HumanSize(Size)
复制代码
作者: Demon 时间: 2011-7-17 12:29
本帖最后由 Demon 于 2011-7-18 12:07 编辑
- Function format_size(size)
- On Error Resume Next 'For Log(0)
- sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB")
- i = Int(Log(size) / Log(1024))
- format_size = Round(size / 1024 ^ i, 2) & sizes(i)
- End Function
-
- 'Example
- Set fso = CreateObject("Scripting.FileSystemObject")
- size = fso.GetFolder(".").Size
- WScript.Echo format_size(size)
复制代码
作者: powerbat 时间: 2011-7-17 13:44
VBS的函数真不省心,默认会省略纯小数(整数部分为0的小数)小数点前面的0,这虽然是合法的小数表示法,但不符合人的习惯。Round函数没有选项改变这种行为,FormatNumber函数可以指定。但FormatNumber不爽的是对纯整数也要添加指定个数的小数位,没有选项可以设置不要添加。
FormatNumber(Size/Gates(i), 2, -1, , 0)
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |