Jump to content

Get InetGetSize in MB/GB


DigDeep
 Share

Recommended Posts

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

 

Link to comment
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

 

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:

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 APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

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 * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

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) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

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 *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"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: 2023-04-24

Link to comment
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

 

Link to comment
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?

Spoiler

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

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

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

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

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.

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

Au3Record.exe

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...