FileSize_Formatted

Returns file size in formated human way (mb, gb, etc..)
Or, it can format any number into filesize format
Or, it can return filesize without formating
Updated 2020-0420: Making it more customizable with tweaks ready to be modified and used in VB, VBA and ASP Classic

CodeFunctionName
What is this?

Public

Tested

Original Work
Function FileSize_Formatted(FullFilePath, orFileSize, Formatted, DecimalPlaces)
' Gets file size in tb, gb, mb, or kb
' Needs either the full path to file, or the filesize
'
' DecimalPlaces = 0 to 4
' Formatted = 0 or 1
' If you passed FullFilePath and just want to get the file size unformatted
' It doesn't make sense using it with orFileSize
' KiloBased = 1000 or 1024, if omitted it will be 1000
'

siz = orFileSize
If FullFilePath > "" Then
Set FSO = CreateObject("Scripting.FileSystemObject")
' Set t = FSO.getfile(Server.MapPath(FullFilePath))
Set t = FSO.getfile(FullFilePath)
siz = t.Size ' 3555 ' FileLen(Server.MapPath(FullFilePath))
Set t = Nothing
End If
If Formatted = 0 Then
FileSize_Formatted = siz
Exit Function
End If
OneTh = 1000
If siz < OneTh Then
t1 = FormatNumber(siz, DecimalPlaces) & "B"
ElseIf siz < OneTh ^ 2 Then
t1 = FormatNumber(siz / OneTh, DecimalPlaces) & "KB" ' Kilobyte
ElseIf siz < OneTh ^ 3 Then
t1 = FormatNumber(siz / OneTh ^ 2, DecimalPlaces) & "MB" ' Megabyte
ElseIf siz < OneTh ^ 4 Then
t1 = FormatNumber(siz / OneTh ^ 3, DecimalPlaces) & "GB" ' Gigabyte
ElseIf siz < OneTh ^ 5 Then
t1 = FormatNumber(siz / OneTh ^ 4, DecimalPlaces) & "TB" ' Terabyte
ElseIf siz < OneTh ^ 6 Then
t1 = FormatNumber(siz / OneTh ^ 5, DecimalPlaces) & "PB" ' Petabyte
ElseIf siz < OneTh ^ 7 Then
t1 = FormatNumber(siz / OneTh ^ 6, DecimalPlaces) & "EB" ' Exabyte
ElseIf siz < OneTh ^ 8 Then
t1 = FormatNumber(siz / OneTh ^ 7, DecimalPlaces) & "ZB" ' Zettabyte
'Elseif siz < 1024^9 then
' t1 = FormatNumber(siz / 1024^8, 1) & "YB" ' Yottabyte
Else
t1 = FormatNumber(siz / OneTh ^ 8, DecimalPlaces) & "YB" ' Yottabyte
End If
FileSize_Formatted = t1
Set FSO = Nothing
End Function

FullFilePath, orFileSize, Formatted

Views 4,917

Downloads 1,459

CodeID
DB ID