I was creating an application where I had to determine how much a text string was in size (bytes, megabytes,) so I decided to create a small Function that writes the string to a temporary file and then calculate the size in bytes. I also added the ability to output the filesize to th largest possible format e.g. if the filesize was 1048576 bytes then the Function (with output enabled) would Return 1 MB, thanks to Spiff59 for _ByteSuffix.
Function:
AutoIt
; #FUNCTION# ====================================================================================================================; Name ..........: _GetStringSize; Description ...: Determine the size in bytes of a text string.; Syntax ........: _GetStringSize($sString[, $iOutput = 0]); Parameters ....: $sString - String to determine the size of.; $iOutput - [optional] Format the bytes to the largest value e.g. 1024 bytes will be returned as 1 KB. Default is False - Unformatted or True - Formatted.; Return values .: Success - Formatted string size.; Failure - None; Author ........: guinness; Remarks........; Thanks to spiff59 for the formatting idea - <a href='http://www.autoitscript.com/forum/topic/116897-folder-sync-tool/page__view__findpost__p__815328' class='bbc_url' title='' rel='norewrite'>http://www.autoitscript.com/forum/topic/116897-folder-sync-tool/page__view__findpost__p__815328</a> & trancexx for using StringLen.; Example .......: Yes; ===============================================================================================================================Func_GetStringSize($sString,$fOutput=Default)LocalConst$aOutput[9]=[' bytes',' KB',' MB',' GB',' TB',' PB',' EB',' ZB',' YB']LocalConst$iUBound=UBound($aOutput)Local$iFileGetSize=StringLen($sString),$iCount=0If$fOutputThenWhile$iFileGetSize>1023$iCount+=1$iFileGetSize/=1024WEnd$iFileGetSize=Round($iFileGetSize,2)&$aOutput[$iCount]EndIfReturn$iFileGetSizeEndFunc;==>_GetStringSize
Example use of Function:
ConsoleWrite(_GetStringSize('How Much Is This Text In Bytes?',False)&@CRLF); String example without format.ConsoleWrite(_GetStringSize('How Much Is This Text In Bytes?',True)&@CRLF); String example with format.LocalConst$sString='You can download the main AutoIt package and other related tools from this page.'&@CRLF&@CRLF&_'AutoIt has been designed to work on Windows 2000, XP, 2003, Windows Vista, Windows Server 2008, Windows 7 '&_'and Windows 2008 R2. With no annoying runtime libraries required!'&@CRLF&@CRLF&_'AutoIt executables and setup programs have been digitally signed by "Jonathan Bennett".'ConsoleWrite(_GetStringSize($sString,False)&@CRLF); Example without formatting.ConsoleWrite(_GetStringSize($sString,True)&@CRLF); Example with formatting.
Any comments, suggestions are welcomed. Thanks.
This was the old Version I created before trancexx pointed out I could do it a more efficient way.
Spoiler
AutoIt
; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7; #FUNCTION# =========================================================================================================; Name...........: _GetStringSize; Description ...: Determine the size in bytes of a text string.; Syntax.........: _GetStringSize($sString, [$iOutput = 0]); Parameters ....: $sString - String to determine the size of.; $iOutput - [Optional] - Format the bytes to the largest value e.g. 1024 bytes will be returned as 1 KB. (Default = 0 - Unformatted & 1 - Formatted.); Requirement(s).: v3.2.2.0 or higher; Return values .: Success - Returns filesize in bytes (formatted or unformatted.); Failure - Returns -1 & sets @error to 1.; Author ........: guinness; Example........; Yes; Remarks........; Thanks to spiff59 for the format idea - <a href='http://www.autoitscript.com/forum/topic/116897-folder-sync-tool/page__view__findpost__p__815328' class='bbc_url' title='' rel='norewrite'>http://www.autoitscript.com/forum/topic/116897-folder-sync-tool/page__view__findpost__p__815328</a>;=====================================================================================================================Func_GetStringSize($sString,$iOutput=0)Local$aOutput[9]=[" B"," KB"," MB"," GB"," TB"," PB","EB","ZB","YB"],$hFileOpen,$iCount,$iFileGetSize,$sFile=@ScriptDir&"_GetStringSize.dat"$hFileOpen=FileOpen($sFile,2)If$hFileOpen=-1ThenFileClose($hFileOpen)ReturnSetError(1,0,-1)EndIfFileWrite($hFileOpen,$sString)FileClose($hFileOpen)$iFileGetSize=FileGetSize($sFile)FileClose(FileOpen($sFile,2))If$iOutputThenWhile$iFileGetSize>1023$iCount+=1$iFileGetSize/=1024WEnd$iFileGetSize=Round($iFileGetSize,2)&$aOutput[$iCount]EndIfFileDelete($sFile)If@errorThenReturnSetError(1,0,$iFileGetSize)EndIfReturn$iFileGetSizeEndFunc;==>_GetStringSize
AutoIt
; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7; #FUNCTION# =========================================================================================================; Name...........: _GetStringSize; Description ...: Determine the size in bytes of a text string.; Syntax.........: _GetStringSize($sString, [$iOutput = 0]); Parameters ....: $sString - String to determine the size of.; $iOutput - [Optional] - Format the bytes to the largest value e.g. 1024 bytes will be returned as 1 KB. (Default = 0 - Unformatted & 1 - Formatted.); Requirement(s).: v3.2.2.0 or higher; Return values .: Success - Returns filesize in bytes (formatted or unformatted.); Failure - Returns -1 & sets @error to 1.; Author ........: guinness; Example........; Yes; Remarks........; Thanks to spiff59 for the format idea - <a href='http://www.autoitscript.com/forum/topic/116897-folder-sync-tool/page__view__findpost__p__815328' class='bbc_url' title='' rel='norewrite'>http://www.autoitscript.com/forum/topic/116897-folder-sync-tool/page__view__findpost__p__815328</a> & smartee.;=====================================================================================================================Func_GetStringSize($sString,$iOutput=0)Local$aOutput[9]=[" B"," KB"," MB"," GB"," TB"," PB","EB","ZB","YB"],$iCount,$iFileGetSize$iFileGetSize=BinaryLen(StringToBinary($sString))If$iOutputThenWhile$iFileGetSize>1023$iCount+=1$iFileGetSize/=1024WEnd$iFileGetSize=Round($iFileGetSize,2)&$aOutput[$iCount]EndIfReturn$iFileGetSizeEndFunc;==>_GetStringSizeEx
Oh well don't I look like a fool by smartee again. I actually didn't think about BinaryLen() I don't know what is wrong with me right now. Thanks for the heads up. I updated the Function above
This only works well for ANSI strings, or more precisely AutoIt strings (Unicode) which contain only characters in your ANSI codepage.
Exabyte (EB), Zettabyte (ZB) & Yottabyte (YB)
Not going to see that anytime soon in Windows
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQL tutorial (covers generic SQL, but most of it apply to SQLite as well)An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious workPCRE v8.32 regexp pattern documentation. AutoIt uses a slightly older version so that more advanced features are not all available.RegExp tutorial: enough to get started
This only works well for ANSI strings, or more precisely AutoIt strings (Unicode) which contain only characters in your ANSI codepage. Not going to see that anytime soon in Windows
that's what I thought to, maybe we'll have to wait for Win15 or something like that ...
File size. I needed it for the PasteBin application due to PasteBin.com enforcing a limit on the '(file) size' of data you can upload, 512 kilobytes to be exact for a free user.