Posted By

Scooter on 03/21/09


Tagged


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

nelda751
benrudolph
higrade


getFileSize


 / Published in: ASP
 

URL: http://reusablecode.blogspot.com/2009/03/getfilesize.html

Returns the file size of the specified file on the filesystem, formatted human-readable in the most logical unit size.

  1. <%
  2. ' Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
  3. '
  4. ' This work is licensed under the Creative Commons Attribution License. To view
  5. ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  6. ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  7. ' 94305, USA.
  8.  
  9. ' Retrieve the file size of a given file.
  10. function getFileSize(someFile)
  11. dim fs
  12. dim file
  13.  
  14. set fs = Server.CreateObject("Scripting.FileSystemObject")
  15. set file = fs.GetFile(Server.MapPath(someFile))
  16.  
  17. getFileSize = FormatFileSize(file.size)
  18. set file = nothing
  19. set fs = nothing
  20. end function
  21.  
  22. ' Format a file size in the most practical units.
  23. ' Input: size in bytes
  24. function FormatFileSize(size)
  25. dim units
  26. dim factor
  27.  
  28. units = Array("B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
  29. factor = log(size) \ 7
  30.  
  31. FormatFileSize = Round(size / (1024 ^ factor), 2) & units(factor)
  32. end function
  33. %>

Report this snippet  

You need to login to post a comment.