DigDeep

Get InetGetSize in MB/GB

5 posts in this topic

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

 

Share this post


Link to post
Share on other sites



; #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

 

1 person likes this

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

@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

 

Share this post


Link to post
Share on other sites

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?

1 person likes this

Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Share this post


Link to post
Share on other sites

Thanks to all and point taken.

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