Board logo

标题: [转贴] VBS计算文件、文件夹大小 [打印本页]

作者: qq692172929    时间: 2011-7-17 00:16     标题: VBS计算文件、文件夹大小

  1. set open=createobject("scripting.filesystemobject")
  2. str=inputbox("请输入文件(夹)名称或文件(夹)完整路径:",wscript.scriptname)
  3. if open.fileexists(str) then
  4. f1=open.getfile(str).size
  5. if f1<=1024 then
  6. sum=f1/1024
  7. t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
  8. else
  9. if f1<=1024^2 then
  10. sum=f1/1024^2
  11. t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
  12. else
  13. sum=f1/1024^2
  14. t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
  15. end if
  16. end if
  17. else
  18. f2=open.getfolder(str).size
  19. if f2<=1024 then
  20. sum=f2/1024
  21. t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
  22. else
  23. if f2<=1024^2 then
  24. sum=f2/1024^2
  25. t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
  26. else
  27. sum=f2/1024^2
  28. t=split(sum,".")(0)&"."&left(split(sum,".")(1),2)
  29. end if
  30. end if
  31. end if
  32. msgbox round(t,2)&" MB",64,wscript.scriptname
复制代码

作者: powerbat    时间: 2011-7-17 10:22

  1. function HumanSize(size)
  2.     Dim Units, Gates, SizeStr
  3.     Units = Array("Byte", "KB", "MB", "GB", "TB")
  4.     Gates = Array(1, 1024, 1024^2, 1024^3, 1024^4)
  5.     SizeStr = "0Byte"
  6.     for i=UBound(Units) to 0 Step -1
  7.         if size>=Gates(i) then
  8.             SizeStr = FormatNumber(Size/Gates(i), 2) & Units(i)
  9.             exit for
  10.         end if
  11.     next
  12. HumanSize = SizeStr
  13. end function
  14. 'example
  15. set fso = CreateObject("Scripting.FileSystemObject")
  16. Size = fso.GetFolder(".").Size
  17. WScript.Echo Size & " = " & HumanSize(Size)
复制代码

作者: Demon    时间: 2011-7-17 12:29

本帖最后由 Demon 于 2011-7-18 12:07 编辑
  1. Function format_size(size)
  2.     On Error Resume Next 'For Log(0)
  3.     sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB")
  4.     i = Int(Log(size) / Log(1024))
  5.     format_size = Round(size / 1024 ^ i, 2) & sizes(i)
  6. End Function
  7. 'Example
  8. Set fso = CreateObject("Scripting.FileSystemObject")
  9. size = fso.GetFolder(".").Size
  10. 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