Sign in to follow this  
Followers 0
amanda089

Possible addition to the File.au3 UDF

3 posts in this topic

#1 ·  Posted (edited)

I would like to submit this as a possible addition to the File.au3 UDF.

;===============================================================================
;
; Description:    Returns the file size in human readable format
; Syntax:          _FileSize( $sFilePath )
; Parameter(s):  $sFilePath     - Path and filename of the file to be read
; Requirement(s):   None
; Return Value(s):  Array:  [0] = Filesize
;                           [1] = 2 char abbreviated designation
;                           [2] = full designation
; Author(s):        Ariana <amanda089 at gmail dot com>
; Note(s):        If $sFilePath is not specified, will use @AutoItExe
;===============================================================================
Func _FileSize($sFilePath = "")
    If $sFilePath = "" Then
    ;Set $sFilePath to @AutoItExe if empty
        $sFilePath = @AutoItExe
    EndIf
    Local $iFileSize = FileGetSize($sFilePath)
    Local $aSizeCalculations[4][3] = _
        [[1024,                     "KB",  "Kilobyte"], _;(KB) Kilobyte     (04 digits)
        [ 1048576,                  "MB",  "Megabyte"], _;(MB) Megabyte     (07 digits)
        [ 1073741824,               "GB",  "Gigabyte"], _;(GB) Gigabyte     (10 digits)
        [ 1099511627776,            "TB",  "Terabyte"]] ;(TB) Terabyte      (13 digits)

;===============================================================================
;The definitions below can not be used in AutoIt as of yet. (> 15 digit precision)
;-------------------------------------------------------------------------------
;~      [ 1125899906842624,         "PB",  "Petabyte"], _;(PB) Petabyte     (16 digits)
;~      [ 1152921504606846976,      "EB",   "Exabyte"], _;(EB) Exabyte      (19 digits)
;~      [ 1180591620717411303424,   "ZB", "Zettabyte"], _;(ZB) Zettabyte        (22 digits)
;~      [ 1208925819614629174706176,"YB", "Yottabyte"]] ;(YB) Yottabyte     (25 digits)
;===============================================================================

    For $i = (UBound($aSizeCalculations)-1) To 0 Step -1
        If $iFileSize >= $aSizeCalculations[$i][0] Then 
            Local $aReturn[3] = [$iFileSize/$aSizeCalculations[$i][0],String($aSizeCalculations[$i][1]),String($aSizeCalculations[$i][2])]
            If ($iFileSize/$aSizeCalculations[$i][0]) > 1 Then $aReturn[2] &= "s" ;Pluralize as needed
            Return $aReturn
        EndIf
    Next
    ;ConsoleWrite("File Size: "&$sFilePath&" = "&$aReturn[0]&$aReturn[1]&" | "&$aReturn[2]&@CRLF)
    ;Return $aReturn
EndFunc

Attached: _FileSize($sFilePath) <_FileSize.au3>

EDIT: changed to [autoit ] tags.

formatting is screwy because of tabs instead of spaces.

EDIT: Updated to fix a bug where it was only returning the size as kilobytes.

_FileSize.au3

Edited by amanda089

Share this post


Link to post
Share on other sites



I would like to hang it on the wall as a artistic pattern to look at. :nuke:

Using [ autoit ] ... [ /autoit ] tags (minus the spaces) may have served better, but you can always edit to fix.

I need a yottabyte of storage to fully test this nice code out for precision (yep, dreaming again :P )

Thanks for sharing.

Share this post


Link to post
Share on other sites

I needed a function to convert byte sizes into human readable terms, so I made this one. I figure people will find use for it in "Explorer" style utilities or in any sort'by'size utils.

I'm currently trying to make a utility to watch the file size of file(s)/folder(s) and am stumped as to why the listview isn't updating, even with an example GuiCtrlCreateListViewItem that worked right after I make the GUI. Probably need to reboot my computer (just have to wait another 6 hours or so until video's finish converting)

Share this post


Link to post
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
Sign in to follow this  
Followers 0