Jump to content

Recommended Posts

Posted

I wanted to get the size of the file both in MB and GB before downloading it.

If I am using the below format just as <MsgBox(0, "", $iFileSize  & " MB")>, it gives me the values. But still in the Bytes format.

Can someone please help in getting the size in MB and GB format. Like If less than 104857600 then 10MB else 1GB.

Local $Size = InetGetSize('http://download.windowsupdate.com/d/msdownload/update/software/secu/2016/11/windows10.0-kb3205386-x64_90dce4e2430a24e5a94e3d6d35883dc19718ab7d.cab')
    If $Size <= 104857600 Then
    MsgBox(0, "", $Size & /2^20 & " MB")
    Else
    MsgBox(0, "", $Size & /2^30 & ' GB')
    EndIf

 

Posted
; #FUNCTION# ====================================================================================================================
; Name ..........: _String_GetFormattedSizeEx
; Description ...: Return ByteSize in human-readable format ("complex format" GB + MB + KB )
; Syntax ........: _String_GetFormattedSizeEx($iByteSize)
; Parameters ....: $iByteSize           - an integer value.
; Return values .: ByteSize in format like this:  7 GB 770 MB 089 KB
; Author ........: mLipok
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _String_GetFormattedSizeEx($iByteSize)
    Local $sKB = _String_GetFormattedSize($iByteSize, 0, 'KB')
    Local $sMB = _String_GetFormattedSize($iByteSize, 0, 'MB')
    Local $sGB = _String_GetFormattedSize($iByteSize, 0, 'GB')
    Return $sGB & ' ' & StringRight($sMB, 6) & ' ' & StringRight($sKB, 6)
EndFunc   ;==>_String_GetFormattedSizeEx

; #FUNCTION# ====================================================================================================================
; Name ..........: _String_GetFormattedSize
; Description ...: Return ByteSize in human-readable format (one of avalaible size format)
; Syntax ........: _String_GetFormattedSize($iByteSize[, $iRound = 2[, $sRetFormat = -1]])
; Parameters ....: $iByteSize           - an integer value.
;                  $iRound              - [optional] an integer value. Default is 2.
;                  $sRetFormat          - [optional] a string value. Default is -1.
; Return values .: ByteSize in choosen format
; Author ........: MrCreatoR
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........: https://www.autoitscript.com/forum/topic/110713-convertfilesize-udf/#comment-777347
; Example .......: No
; ===============================================================================================================================
Func _String_GetFormattedSize($iByteSize, $iRound = 2, $sRetFormat = -1)
    Local $asBytes[9] = [8, 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] ;Last two unreachable ;)
    Local $iBytes_Val = 2 ^ 10

    If $iByteSize < $iBytes_Val Then Return $iByteSize & ' Bytes'

    Local $iRetFormat_IsString = IsString($sRetFormat)
    Local $iFor = 1, $iTo = 8, $iStep = 1

    If $iRetFormat_IsString Then
        If Not StringRegExp($sRetFormat, "\A(?i)(KB|MB|GB|TB|PB|EB|ZB|YB)\z") Then Return SetError(1, 0, $iByteSize)
    Else
        $iFor = 8
        $iTo = 1
        $iStep = -1
    EndIf

    For $i = $iFor To $iTo Step $iStep
        If ($iRetFormat_IsString And $sRetFormat = $asBytes[$i]) Or _
                (Not $iRetFormat_IsString And $iByteSize >= $iBytes_Val ^ $i) Then
            Return Round($iByteSize / $iBytes_Val ^ $i, $iRound) & ' ' & $asBytes[$i]
        EndIf
    Next
EndFunc   ;==>_String_GetFormattedSize

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

@mLipok

Thank you for your quick help. however it looks too big here just to get the result in MB / GB. My idea was to go to the URL and just check if it is in MB or GB size so that i can take the desired actions.

I tried another way here too which works so far what I need. Is there anyway we can simplify this a little more shorter that it will not make my code huge.

Local $iFileSize = InetGetSize('http://download.windowsupdate.com/d/msdownload/update/software/secu/2016/11/windows10.0-kb3205386-x64_90dce4e2430a24e5a94e3d6d35883dc19718ab7d.cab')
$size1 = Round($iFileSize/1024/1024,2)
$size2 = Round($iFileSize/1024/1024/1024,2)
    If $iFileSize < 104857600 Then
    MsgBox(0, "", $size1 & " MB")
    Else
    MsgBox(0, "", $size2 & " GB")
    EndIf

 

Posted

Why do you care about the size? you're not carrying it around, are you? :P

What Mlipok posted can be used as udf, so it's not even in the main script, you just call the function..

You basically ignored the code, and proceeded to "improve" your way.

Local $iFileSize = InetGetSize('http://download.windowsupdate.com/d/msdownload/update/software/secu/2016/11/windows10.0-kb3205386-x64_90dce4e2430a24e5a94e3d6d35883dc19718ab7d.cab')
    If $iFileSize < 104857600 Then
    MsgBox(0, "", Round($iFileSize/1024/1024,-1) & " MB")
    Else
    MsgBox(0, "", Round($iFileSize/1024/1024/1024,2) & " GB")
    EndIf

shorter by 2 lines, who cares?

  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

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
  • Recently Browsing   0 members

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