Jump to content

_ConvertFileSize


AZJIO
 Share

Recommended Posts

Trying to make analogue of StrFormatByteSize

999, 1KB...999KB, 1MB...999MB, 1GB

1024 --> 1000

#include <Array.au3>
$Size1=StringSplit('500,3456,45649,567413,9785456,67549104,107204433,9058236247,59432545309,835068163432,7342438591324,25239564534854,598452346404443', ',')
Global $Size[$Size1[0]+1][2]
$Size[0][0]=$Size1[0]

For $i = 1 to $Size1[0]
$Size[$i][0]=$Size1[$i]
$Size[$i][1]=_ConvertFileSize($Size1[$i])
Next
_ArrayDisplay($Size)

Func _ConvertFileSize($iBytes)
Local Const $iConst = 0.0000234375 ; (1024 / 1000 - 1) / 1024
Switch $iBytes
Case 110154232090684 To 1125323453007766
$iBytes = Round($iBytes / (1099511627776 + $iBytes * $iConst)) & ' TB'
Case 1098948684577 To 110154232090683
$iBytes = Round($iBytes / (1099511627776 + $iBytes * $iConst), 1) & ' TB'
Case 107572492277 To 1098948684576
$iBytes = Round($iBytes / (1073741824 + $iBytes * $iConst)) & ' GB'
Case 1073192075 To 107572492276
$iBytes = Round($iBytes / (1073741824 + $iBytes * $iConst), 1) & ' GB'
Case 105156613 To 1073192074
$iBytes = Round($iBytes / (1048576 + $iBytes * $iConst)) & ' MB'
Case 1048040 To 105156612
$iBytes = Round($iBytes / (1048576 + $iBytes * $iConst), 1) & ' MB'
Case 102693 To 1048039
$iBytes = Round($iBytes / (1024 + $iBytes * $iConst)) & ' KB'
Case 1024 To 102692
$iBytes = Round($iBytes / (1024 + $iBytes * $iConst), 1) & ' KB'
Case 0 To 1023
$iBytes = Int($iBytes / 1.024)
EndSwitch
Return $iBytes
EndFunc ;==>_ConvertFileSize

#include <File.au3>
#include <GUIConstantsEx.au3>
#Include <GuiListView.au3>

$aFiles = _FileListToArray (@SystemDir, "*.*", 1)

GUICreate ("FileSize")
$ListView = GUICtrlCreateListView ("File|Size", 5, 5, 385, 390, -1, $LVS_EX_FULLROWSELECT)
_GUICtrlListView_SetColumnWidth ($ListView, 0, 280)
_GUICtrlListView_SetColumnWidth ($ListView, 1, 70)
GUISetState()

For $i = 1 To $aFiles[0]
GUICtrlCreateListViewItem ($aFiles[$i] & "|" & _ConvertFileSize(FileGetSize (@SystemDir&''&$aFiles[$i])), $ListView)
Next

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _ConvertFileSize($iBytes)
Local Const $iConst = 0.0000234375 ; (1024 / 1000 - 1) / 1024
Switch $iBytes
Case 110154232090684 To 1125323453007766
$iBytes = Round($iBytes / (1099511627776 + $iBytes * $iConst)) & ' TB'
Case 1098948684577 To 110154232090683
$iBytes = Round($iBytes / (1099511627776 + $iBytes * $iConst), 1) & ' TB'
Case 107572492277 To 1098948684576
$iBytes = Round($iBytes / (1073741824 + $iBytes * $iConst)) & ' GB'
Case 1073192075 To 107572492276
$iBytes = Round($iBytes / (1073741824 + $iBytes * $iConst), 1) & ' GB'
Case 105156613 To 1073192074
$iBytes = Round($iBytes / (1048576 + $iBytes * $iConst)) & ' MB'
Case 1048040 To 105156612
$iBytes = Round($iBytes / (1048576 + $iBytes * $iConst), 1) & ' MB'
Case 102693 To 1048039
$iBytes = Round($iBytes / (1024 + $iBytes * $iConst)) & ' KB'
Case 1024 To 102692
$iBytes = Round($iBytes / (1024 + $iBytes * $iConst), 1) & ' KB'
Case 0 To 1023
$iBytes = Int($iBytes / 1.024)
EndSwitch
Return $iBytes
EndFunc ;==>_ConvertFileSize
Edited by AZJIO
Link to comment
Share on other sites

Another way without using the API StrFormatByteSize, I discovered this gem by I haven't tested the speed difference with yours and mine, but it would be interesting to see what they are.

Func _ByteSuffix($iBytes, $iRound = 2)
    Local $A, $aArray[9] = [" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]
    While $iBytes > 1023
        $A += 1
        $iBytes /= 1024
    WEnd
    Return Round($iBytes, $iRound) & $aArray[$A]
EndFunc   ;==>_ByteSuffix

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

guinness

The difference is 3 times

Local $iConvert = Random(0, 2147483647, 1)

$timer = TimerInit()
For $A = 1 To 100000
_ByteSuffix($iConvert)
Next
MsgBox(0,"", 't = '&Round(TimerDiff($timer) / 1000, 2) & ' sec')

Local $timer = TimerInit()
For $A = 1 To 100000
_ConvertFileSize($iConvert)
Next
MsgBox(0,"", 't = '&Round(TimerDiff($timer) / 1000, 2) & ' sec')

Func _ByteSuffix($iBytes, $iRound = 2)
Local $A, $aArray[9] = [" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]
While $iBytes > 1023
$A += 1
$iBytes /= 1024
WEnd
Return Round($iBytes, $iRound) & $aArray[$A]
EndFunc ;==>_ByteSuffix

Func _ConvertFileSize($iBytes)
Local Const $iConst = 0.0000234375 ; (1024 / 1000 - 1) / 1024
Switch $iBytes
Case 110154232090684 To 1125323453007766
$iBytes = Round($iBytes / (1099511627776 + $iBytes * $iConst)) & ' TB'
Case 1098948684577 To 110154232090683
$iBytes = Round($iBytes / (1099511627776 + $iBytes * $iConst), 1) & ' TB'
Case 107572492277 To 1098948684576
$iBytes = Round($iBytes / (1073741824 + $iBytes * $iConst)) & ' GB'
Case 1073192075 To 107572492276
$iBytes = Round($iBytes / (1073741824 + $iBytes * $iConst), 1) & ' GB'
Case 105156613 To 1073192074
$iBytes = Round($iBytes / (1048576 + $iBytes * $iConst)) & ' MB'
Case 1048040 To 105156612
$iBytes = Round($iBytes / (1048576 + $iBytes * $iConst), 1) & ' MB'
Case 102693 To 1048039
$iBytes = Round($iBytes / (1024 + $iBytes * $iConst)) & ' KB'
Case 1024 To 102692
$iBytes = Round($iBytes / (1024 + $iBytes * $iConst), 1) & ' KB'
Case 0 To 1023
$iBytes = Int($iBytes / 1.024)
EndSwitch
Return $iBytes
EndFunc ;==>_ConvertFileSize

czardas

I've been doing for the files, but then I thought that you can apply to the size of HDD, and showed that in this case the function is not suitable

Edited by AZJIO
Link to comment
Share on other sites

Your timer test doesn't even work because your function is using a ByRef variable and secondly you're not using the example I posted in the 2nd post, thus making this comparison a little unfair. Here is a test comparison which I would say is a little fairer.

Conclusion: Yours is faster, fair enough, but as you mentioned is limited to a certain size.

Local $iConvert = Random(0, 2147483647, 1)

Local $iTimer = TimerInit()
For $A = 1 To 1000
    _ByteSuffix($iConvert)
Next
ConsoleWrite(TimerDiff($iTimer) & @LF)

$iTimer = TimerInit()
For $A = 1 To 1000
    _ConvertFileSize($iConvert)
Next
ConsoleWrite(TimerDiff($iTimer) & @LF)

Func _ByteSuffix($iBytes, $iRound = 2)
    Local $A, $aArray[9] = [" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]
    While $iBytes > 1023
        $A += 1
        $iBytes /= 1024
    WEnd
    Return Round($iBytes, $iRound) & $aArray[$A]
EndFunc   ;==>_ByteSuffix

Func _ConvertFileSize($iBytes)
    Local Const $iGMK = (1024 / 1000 - 1) / 1024
    Switch $iBytes
        Case 107572492277 To 1099511627775
            $iBytes = Round($iBytes / (1073741824 + $iBytes * $iGMK)) & ' GB'
        Case 1073192075 To 107572492276
            $iBytes = Round($iBytes / (1073741824 + $iBytes * $iGMK), 1) & ' GB'
        Case 105156613 To 1073192074
            $iBytes = Round($iBytes / (1048576 + $iBytes * $iGMK)) & ' MB'
        Case 1048040 To 105156612
            $iBytes = Round($iBytes / (1048576 + $iBytes * $iGMK), 1) & ' MB'
        Case 102693 To 1048039
            $iBytes = Round($iBytes / (1024 + $iBytes * $iGMK)) & ' KB'
        Case 1024 To 102692
            $iBytes = Round($iBytes / (1024 + $iBytes * $iGMK), 1) & ' KB'
        Case 0 To 1023
            $iBytes = Int($iBytes / 1.024)
    EndSwitch
    Return $iBytes
EndFunc   ;==>_ConvertFileSize
Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

guinness

another test

#include <GUIConstantsEx.au3>
#Include <GuiListView.au3>

Global $Size[26]=[500, 1023, 1024, 101887, 101888, 102589, 102590, 1047552, 1047553, 1048039, 1048040, 104333311, 104333312, 105051261, 105051262, 1072693248, 1072693249, 1073192074, 1073192075, 107572492276, 107572492277, 1098948684576, 1098948684577, 110154232090683, 110154232090684, 1125323453007766]

GUICreate ("FileSize")
$ListView = GUICtrlCreateListView ("Size|_ByteSuffix|_ConvertFileSize", 5, 5, 385, 390, -1, $LVS_EX_FULLROWSELECT)
_GUICtrlListView_SetColumnWidth ($ListView, 0, 140)
GUISetState()

For $i = 0 To 25
GUICtrlCreateListViewItem ($Size[$i] & "|" &_ByteSuffix($Size[$i], 0)& "|" & _ConvertFileSize($Size[$i]), $ListView)
Next

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _ConvertFileSize($iBytes)
Local Const $iConst = 0.0000234375 ; (1024 / 1000 - 1) / 1024
Switch $iBytes
Case 110154232090684 To 1125323453007766
$iBytes = Round($iBytes / (1099511627776 + $iBytes * $iConst)) & ' TB'
Case 1098948684577 To 110154232090683
$iBytes = Round($iBytes / (1099511627776 + $iBytes * $iConst), 1) & ' TB'
Case 107572492277 To 1098948684576
$iBytes = Round($iBytes / (1073741824 + $iBytes * $iConst)) & ' GB'
Case 1073192075 To 107572492276
$iBytes = Round($iBytes / (1073741824 + $iBytes * $iConst), 1) & ' GB'
Case 105156613 To 1073192074
$iBytes = Round($iBytes / (1048576 + $iBytes * $iConst)) & ' MB'
Case 1048040 To 105156612
$iBytes = Round($iBytes / (1048576 + $iBytes * $iConst), 1) & ' MB'
Case 102693 To 1048039
$iBytes = Round($iBytes / (1024 + $iBytes * $iConst)) & ' KB'
Case 1024 To 102692
$iBytes = Round($iBytes / (1024 + $iBytes * $iConst), 1) & ' KB'
Case 0 To 1023
$iBytes = Int($iBytes / 1.024)
EndSwitch
Return $iBytes
EndFunc ;==>_ConvertFileSize

Func _ByteSuffix($iBytes, $iRound = 2)
Local $A, $aArray[9] = [" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]
While $iBytes > 1023
$A += 1
$iBytes /= 1024
WEnd
Return Round($iBytes, $iRound) & $aArray[$A]
EndFunc ;==>_ByteSuffix
Edited by AZJIO
Link to comment
Share on other sites

Change this _ByteSuffix($Size[$i]) to this _ByteSuffix($Size[$i], 0). Thanks for the example.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 1 year later...

Another way without using the API StrFormatByteSize

This is the second time I was able to do a WinAPI function call. My function is 3 times faster than the WinAPI.

$sRes = _WinAPI_StrFormatByteSize(475) & @LF
$sRes &= _WinAPI_StrFormatByteSize(2345) & @LF
$sRes &= _WinAPI_StrFormatByteSize(10457) & @LF
$sRes &= _WinAPI_StrFormatByteSize(334987) & @LF
$sRes &= _WinAPI_StrFormatByteSize(4958283) & @LF
$sRes &= _WinAPI_StrFormatByteSize(67856785) & @LF
$sRes &= _WinAPI_StrFormatByteSize(5668769783) & @LF
$sRes &= _WinAPI_StrFormatByteSize(65786786443) & @LF
$sRes &= _WinAPI_StrFormatByteSize(876463256876) & @LF
MsgBox(0, 'Short', $sRes)

Func _WinAPI_StrFormatByteSize($Size)
    $tBuffer = DllStructCreate("char[256]")
    Local $aResult = DLLCall("shlwapi.dll", "str", "StrFormatByteSize", "DWORD", $Size, "ptr", DllStructGetPtr($tBuffer), "int", DllStructGetSize($tBuffer))
    If @error Then Return SetError(@error, @extended, -1)
    Return $aResult[0]
EndFunc   ;==>_WinAPI_StrFormatByteSize
Edited by AZJIO
Link to comment
Share on other sites

OK, but function is in WinAPIEx. Thanks for testing though.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

For who? Your function of the Win32 API?

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

@AZJIO
Thanks for
_ConvertFileSize, really good Function

@All

is better use of the _WinAPI_StrFormatByteSize(), I recently had a fate function of this kind, always with the intention to do it faster ect ect, but it will never 100% choir, because you know the history of microsoft windows in its own way in knowing certain size '?do=embed' frameborder='0' data-embedContent>>

Legolash2o writes

1GB = 1000MB
1GiB = 1024MiB

Microsoft is the only company to use GiB which is why HDD appear smaller than advertised, i.e. A 500GB HDD in Windows shows as around 465GB but if you check your HDD via linux or Mac is will show 500GB.

As i said microsoft uses 1024 instead of 1000 like everyone else but confusingly they still show files/folders as GB/MB instead of the correct GiB/MiB.

500GB is around 465GiB, but Microsoft change GiB to GB which is incorrect.

so always best to use _WinAPI_StrFormatByteSize() to always have a choir information in Windows System (or to say more good to be always synchronized properly with Windows)

or monitored well a certain thing, because I'm done with AutoIt this tool long ago (which is a clone of the windows explorer)

Ciao.

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Link to comment
Share on other sites

@All

is better use of the _WinAPI_StrFormatByteSize()

I did not set out to use the wrong calculation. I set a goal to calculate correctly, but round the number so that the numbers were not the four-digit.

1000 b = 0.98 KB (1000/1024)

By the way, I added this function in >FileOperations.au3. I changed the bottom limit that bytes were exact

 

For who? Your function of the Win32 API?

Sure API

 

Comparison

#include <Array.au3>
#include <FileOperations.au3>

Local $Size[13] = [500, 3456, 45649, 567413, 9785456, 67549104, 107204433, 9058236247, 59432545309, 835068163432, 7342438591324, 25239564534854, 598452346404443]
$sRes = '_FO_Sht' & @TAB & 'WinAPI' & @TAB & '_ByteSuffix' & @TAB & 'original' & @LF
For $i = 0 To 12
    $sRes &= _FO_ShortFileSize($Size[$i]) & @TAB
    $sRes &= _WinAPI_StrFormatByteSize($Size[$i]) & @TAB
    $sRes &= _ByteSuffix($Size[$i]) & @TAB
    $sRes &= StringRegExpReplace($Size[$i], '(\A\d{1,3}(?=(\d{3})+\z)|\d{3}(?=\d))', '\1 ') & @LF
Next
MsgBox(0, 'Сообщение', $sRes)

Func _ByteSuffix($iBytes)
    Local $A, $aArray[9] = [" B ", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"]
    While $iBytes > 1023
        $A += 1
        $iBytes /= 1024
    WEnd
    Return StringFormat('%-8.3f %s', Round($iBytes, 3), $aArray[$A])
EndFunc   ;==>_ByteSuffix

Func _WinAPI_StrFormatByteSize($Size)
    $tBuffer = DllStructCreate("char[512]")
    Local $aResult = DLLCall("shlwapi.dll", "str", "StrFormatByteSize", "DWORD", $Size, "ptr", DllStructGetPtr($tBuffer), "int", DllStructGetSize($tBuffer))
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[0]
EndFunc   ;==>_WinAPI_StrFormatByteSize
Edited by AZJIO
Link to comment
Share on other sites

Hi AZJIO, I know that you the fate the best there, and Thanks again for the _ConvertFileSize, but being a situation a little confused and uncertain (of course not your fault, but the fault of microsoft), best of microsoft programmers who know the code of the Windows ehhhh no one else will know and interpret better this function, precisely for this reason to always be 100% sure in this case is recommended the use of the _WinAPI_StrFormatByteSize() (as always all is just my opinion)

Ciao.

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Link to comment
Share on other sites

DXRW4E

This is what you wanted.

Func _FO_ShortFileSize($iBytes)
    Switch $iBytes
        Case 10995116277760 To 109951162777600 ; 10 - 100 TB
            $iBytes = Round($iBytes / 1099511627776, 1) & ' TB'
        Case 1000000000000 To 10995116277759 ; 1000 GB - 10 TB
            $iBytes = Round($iBytes / 1099511627776, 2) & ' TB'
        Case 107374182400 To 999999999999 ; 100 - 999 GB
            $iBytes = Round($iBytes / 1073741824) & ' GB'
        Case 10737418240 To 107374182399 ; 10 - 100 GB
            $iBytes = Round($iBytes / 1073741824, 1) & ' GB'
        Case 1000000000 To 10737418239 ; 1000 MB - 10 GB
            $iBytes = Round($iBytes / 1073741824, 2) & ' GB'
        Case 104857600 To 999999999 ; 100 - 999 MB
            $iBytes = Round($iBytes / 1048576) & ' MB'
        Case 10485760 To 104857599 ; 10 - 100 MB
            $iBytes = Round($iBytes / 1048576, 1) & ' MB'
        Case 1000000 To 10485759 ; 1000 KB - 10 MB
            $iBytes = Round($iBytes / 1048576, 2) & ' MB'
        Case 102400 To 999999 ; 100 - 999 KB
            $iBytes = Round($iBytes / 1024) & ' KB'
        Case 10240 To 102399 ; 10 - 100 KB
            $iBytes = Round($iBytes / 1024, 1) & ' KB'
        Case 1000 To 10239 ; 1000 B - 10 KB
            $iBytes = Round($iBytes / 1024, 2) & ' KB'
        Case 0 To 999
            $iBytes &= ' B'
    EndSwitch
    Return $iBytes
EndFunc   ;==>_FO_ShortFileSize

.

Edited by AZJIO
Link to comment
Share on other sites

Hi AZJIO, believe it or not but the function done by me long ago, it was almost 99% the same as yours (I really like the Switch-EndSwitch, because the tests fates from me is 2 times faster than the if-elseif ect ect), do not get me wrong, you're not doing anything wrong indeed you done everything very well, but for the reasons said above in this case and better to use the windows API is much safer

Ciao.

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Link to comment
Share on other sites

DXRW4E

The problem is that, instead of 8 GB, it shows 446 MB

ConsoleWrite(_WinAPI_StrFormatByteSize(1073741824) & @LF)
ConsoleWrite(_WinAPI_StrFormatByteSize(8589934592) & @LF)

;~ 1.073.741.824
;~ 8.589.934.592

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_StrFormatByteSize
; Description....: Converts a numeric value into a string that represents the number expressed as a size value in bytes, kilobytes, megabytes, or gigabytes.
; Syntax.........: _WinAPI_StrFormatByteSize ( $iSize [, $iFlag] )
; Parameters.....: $iSize  - The numeric value to be converted.
; Return values..: Success - The converted string.
;                  Failure - Empty string and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: None
; Related........:
; Link...........: @@MsdnLink@@ StrFormatByteSizeEx
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_StrFormatByteSize($iSize)

    Local $tData = DllStructCreate('wchar[80]')
    Local $Ret = DllCall('shlwapi.dll', 'ptr', 'StrFormatByteSizeW', 'uint64', $iSize, 'ptr', DllStructGetPtr($tData), 'uint', 80)

    If (@error) Or (Not $Ret[0]) Then
        Return SetError(1, 0, '')
    EndIf
    Return DllStructGetData($tData, 1)
EndFunc   ;==>_WinAPI_StrFormatByteSize

--> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop

1,00 GB

8,00 GB

?????

Ciao.

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Link to comment
Share on other sites

DXRW4E

Why is it safer? Why give things a larger value than it actually is?

and how this can be thousands++++ of such scenarios ect ect

ConsoleWrite("StrFormatByteSize - " & _WinAPI_StrFormatByteSize(1093741824) & @LF)
ConsoleWrite("StrFormatByteSize - " & _WinAPI_StrFormatByteSize(8789934592) & @LF)

ConsoleWrite("_FO_ShortFileSize - " & _FO_ShortFileSize(1093741824) & @LF)
ConsoleWrite("_FO_ShortFileSize - " & _FO_ShortFileSize(8789934592) & @LF)

;~ 1.073.741.824
;~ 8.589.934.592

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_StrFormatByteSize
; Description....: Converts a numeric value into a string that represents the number expressed as a size value in bytes, kilobytes, megabytes, or gigabytes.
; Syntax.........: _WinAPI_StrFormatByteSize ( $iSize [, $iFlag] )
; Parameters.....: $iSize  - The numeric value to be converted.
; Return values..: Success - The converted string.
;                  Failure - Empty string and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: None
; Related........:
; Link...........: @@MsdnLink@@ StrFormatByteSizeEx
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_StrFormatByteSize($iSize)

    Local $tData = DllStructCreate('wchar[80]')
    Local $Ret = DllCall('shlwapi.dll', 'ptr', 'StrFormatByteSizeW', 'uint64', $iSize, 'ptr', DllStructGetPtr($tData), 'uint', 80)

    If (@error) Or (Not $Ret[0]) Then
        Return SetError(1, 0, '')
    EndIf
    Return DllStructGetData($tData, 1)
EndFunc   ;==>_WinAPI_StrFormatByteSize


Func _FO_ShortFileSize($iBytes)
    Switch $iBytes
        Case 10995116277760 To 109951162777600 ; 10 - 100 TB
            $iBytes = Round($iBytes / 1099511627776, 1) & ' TB'
        Case 1000000000000 To 10995116277759 ; 1000 GB - 10 TB
            $iBytes = Round($iBytes / 1099511627776, 2) & ' TB'
        Case 107374182400 To 999999999999 ; 100 - 999 GB
            $iBytes = Round($iBytes / 1073741824) & ' GB'
        Case 10737418240 To 107374182399 ; 10 - 100 GB
            $iBytes = Round($iBytes / 1073741824, 1) & ' GB'
        Case 1000000000 To 10737418239 ; 1000 MB - 10 GB
            $iBytes = Round($iBytes / 1073741824, 2) & ' GB'
        Case 104857600 To 999999999 ; 100 - 999 MB
            $iBytes = Round($iBytes / 1048576) & ' MB'
        Case 10485760 To 104857599 ; 10 - 100 MB
            $iBytes = Round($iBytes / 1048576, 1) & ' MB'
        Case 1000000 To 10485759 ; 1000 KB - 10 MB
            $iBytes = Round($iBytes / 1048576, 2) & ' MB'
        Case 102400 To 999999 ; 100 - 999 KB
            $iBytes = Round($iBytes / 1024) & ' KB'
        Case 10240 To 102399 ; 10 - 100 KB
            $iBytes = Round($iBytes / 1024, 1) & ' KB'
        Case 1000 To 10239 ; 1000 B - 10 KB
            $iBytes = Round($iBytes / 1024, 2) & ' KB'
        Case 0 To 999
            $iBytes &= ' B'
    EndSwitch
    Return $iBytes
EndFunc   ;==>_FO_ShortFileSize

 

>"C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:UsersDXRW4EDesktopNuovo AutoIt v3 Script.au3" /UserParams    

+>05:15:49 Starting AutoIt3Wrapper v.2.1.2.9    Environment(Language:0409  Keyboard:00000409  OS:WIN_8/  CPU:X64 OS:X64)

>Running AU3Check (1.54.22.0)  from:C:Program Files (x86)AutoIt3

+>05:15:49 AU3Check ended.rc:0

>Running:(3.3.8.1):C:Program Files (x86)AutoIt3autoit3.exe "C:UsersDXRW4EDesktopNuovo AutoIt v3 Script.au3"    

--> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop

StrFormatByteSize - 1,01 GB

StrFormatByteSize - 8,18 GB

_FO_ShortFileSize - 1.02 GB

_FO_ShortFileSize - 8.19 GB

+>05:15:49 AutoIt3.exe ended.rc:0

>Exit code: 0    Time: 0.323

 

Please do not misunderstand me, I'm not criticizing, I repeat you did a great job there, but as I said for the reasons stated above

best of microsoft programmers who know the code of the Windows ehhhh no one else will know and interpret better this function

the WinAPI (in this case) and more choiraccuratesecuresafer, and I repeat no fault of yours, but for microsoft's fault that created a bit of confusion there

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

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