Jump to content

_Filesize() Function


busysignal
 Share

Recommended Posts

With a bit of inspiration from qafrost's code and a bit of tweaking I put created a simple function that allows you for format the output of any filesize.

;===============================================================================
; Author(s):        busysignal
; Version:          1.0.0
; AutoItVer:        3.1.1.+
; Created           2005-10-15
;
; Description:    Formats a files size according to $sFormat & $iDecimal parameters
; Syntax:          _Filesize($iValue, $sFormat, $iNum)
;
; Parameter(s):     $iValue = The value in "Bytes"
;                   $SFormat = Specify the Size format you wish to display, options are:
;                   ""      - Auto Select based on value entered
;                   "GB"    - Display in Gigabytes
;                   "MB"    - Display in Megabytes
;                   "KB"    - Display in Kilobytes
;                   "B"     - Display in Bytes
;                   $iDecimal = The number of decimals places to round up converted value to
; Requirement(s):   None
; Return Value(s):  On Success - Returns a string representation of $iValue formatted according to $sFormat
;                   On Failure - Returns an empty string "" if no files are found and sets @Error on errors
;                       @Error=1 $iValue = 0; no value to convert
;                       @Error=2 Either $iValue or $iDecimal contains non-numeric characters.
; Note(s):          Example: _Filesize(2152971234,"GB", 2)  would return "2.15 GB"
;
; Credits:          Inspired by _PictureUDF() by SolidSnake <MetalGearX91@Hotmail.com>
;===============================================================================
Func _Filesize($iValue, $sFormat, $iDecimal)
    Local $sReturn, $iB, $iKB, $iMB, $iGB
    If $iValue = 0 Then
        SetError(1)
        Return 0
    EndIf
    If Not StringIsDigit($iValue) Or Not StringIsDigit($iDecimal) Then
        SetError(2)
        Return 0
    EndIf
; Conversion Chart
    $iB = $iValue
    $iKB = Round($iB / 1024, $iDecimal)
    $iMB = Round($iKB / 1024, $iDecimal)
    $iGB = Round($iMB / 1024, $iDecimal)

    Select
        Case $sFormat = ""; Auto Select Format Display Type
            If $iMB > 1024 Then
                $iValue = $iGB & " GB"
            ElseIf $iKB > 1024 Then
                $iValue = $iMB & " MB"
            ElseIf $iKB > 0 Then
                $iValue = $iKB & " KB"
            ElseIf $iKB < 0 Then
                $iValue = $iB & " Bytes"
            EndIf
        Case $sFormat = "GB"
            $iValue = $iGB & " GB"
        Case $sFormat = "MB"
            $iValue = $iMB & " MB"
        Case $sFormat = "KB"
            $iValue = $iKB & " KB"
        Case $sFormat = "B"
            $iValue = $iB & " Bytes"                
    EndSelect

    Return $iValue
    
EndFunc  ;==>_Filesize

Give it a try! Bugs, Comments, Question please post.

Cheers.. ;)

Link to comment
Share on other sites

Very nice, busysignal. Your UDF has good structure. I do like the method of return and error handling. With good scripting ability like this, I am sure we will see more good UDFs from you.

5 stars for presentation of nice written code. ;)

Link to comment
Share on other sites

Nice work busysignal! I would suggest modifying the code not to error out when 0 is passed as a size though -- returning something like '0 B', '0 KB' etc. would be reasonable.

Link to comment
Share on other sites

@MHz, thanks for the kudo points. It is amazing that it took me a 10 mins to make it and 20 mins to refine it. Strange how that works!

@LxP, I have to agree. But at 3:30am or when ever I posted it I took the short cut on the return param. I like the idea of the zero return. I will have to implement that one..

Cheers.. ;)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...