FormatBytes()

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: FormatBytes()

Post by jNizM » 14 Jul 2016, 05:30

Windows 10.0.14388
2400016 Bytes => 2.28 MB (so still 1024)

M$ should move his WinAPI docs to GitHub...
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: FormatBytes()

Post by arcticir » 14 Jul 2016, 07:54

Code: Select all

FormatBytes(bytes){
	Loop,Parse,bytes|KB|MB|GB|TB|PB|EB|ZB|YB,|
		If bytes>999,bytes/=1024
		else return RTrim(SubStr(bytes,1,4),".") " " A_LoopField
}

Code: Select all

FormatBytes(bytes){
	Loop
		If bytes>999,bytes/=1024
		else return RTrim(SubStr(bytes,1,4),".") " " SubStr("BYKBMBGBTBPBEBZBYB",A_Index*2-1,2)
}
Last edited by arcticir on 14 Jul 2016, 09:51, edited 1 time in total.

User avatar
joedf
Posts: 9000
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: FormatBytes()

Post by joedf » 14 Jul 2016, 08:50

Whewwww! :dance:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: FormatBytes()

Post by lexikos » 14 Jul 2016, 21:46

arcticir's code does not work on AutoHotkey v1, only v2.

Here's another version.

Code: Select all

FormatBytes(bytes){
	Loop
		If bytes > 999
			bytes /= 1024.0
		else return RTrim(SubStr(bytes,1,4),".") " " (A_Index=1 ? "bytes" : SubStr(" KMGTPEZY",A_Index,1) "B")
}

User avatar
joedf
Posts: 9000
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: FormatBytes()

Post by joedf » 14 Jul 2016, 23:01

Nice :)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

ozzii
Posts: 486
Joined: 30 Oct 2013, 06:04

Re: FormatBytes()

Post by ozzii » 15 Jul 2016, 04:22

Yes it is nice ;)

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

FormatBytes() - Updated

Post by SKAN » 30 Apr 2020, 15:40

Previous code:

Code: Select all

FormatBytes( Integer, Suffix := "" ) { ; SKAN,  http://ahkscript.org/boards/viewtopic.php?p=18328#p18328
Static Var := "0123456789ABCDEF" 
Return DllCall( "Shlwapi\StrFormatByteSize64A", "Int64",Integer, "Str",Var, "UInt",16, "AStr" ) . Suffix
}


is now simpliified to a one liner:

Code: Select all

FormatBytes(N) { ; By SKAN on CT5H/D351 @ tiny.cc/formatbytes
Return DllCall("Shlwapi\StrFormatByteSize64A", "Int64",N, "Str",Format("{:16}",N), "Int",16, "AStr") 
}
My Scripts and Functions: V1  V2

User avatar
joedf
Posts: 9000
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: FormatBytes()

Post by joedf » 30 Apr 2020, 21:39

Neat work as always! :+1:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: FormatBytes()

Post by haichen » 01 May 2020, 11:06

2**1024-1 = 7,99 EB
Cool.

hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: FormatBytes()

Post by hasantr » 19 Mar 2022, 15:14

I made a Function to reverse this operation but it doesn't work on small numbers. If you have a better one, it would be great if you could share it. :)

Code: Select all

ToByte(SizeAndSpaceAndUnit){ ;by hasantr
	static Units := {KB:1,MB:2,GB:3,TB:4}
	SplitN := StrSplit(SizeAndSpaceAndUnit,A_Space,"`n") 
	Loop % Units[SplitN.2]
		SplitN.1 := SplitN.1 * 1024
	Return SplitN.1
}

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: FormatBytes()

Post by haichen » 20 Mar 2022, 09:07

Does this help?

Code: Select all

n:=9223372036854775807 ; B = 7,99 EB
;n:=200
x:= FormatBytes(n) 
m:= FormatStringToByte(x) 

tooltip, % n " B = " x " = " m " B" 
sleep,4000
return

FormatStringToByte(unitstrg){ 
    Units := {B:0, Bytes:0, KB:10, MB:20, GB:30,TB:40, PB:50, EB:60}
    RegExMatch(unitstrg, "im)(\d+,?\d{0,})\s{0,}(Bytes|KB|MB|GB|TB|PB|EB)",SubPat)
    SubPat1:=StrReplace(SubPat1,",",".")
    Return round(subPat1 * 2**Units[subPat2])
}

FormatBytes(N) { ; By SKAN on CT5H/D351 @ tiny.cc/formatbytes
Return DllCall("Shlwapi\StrFormatByteSize64A", "Int64",N, "Str",Format("{:16}",N), "Int",16, "AStr") 
}



hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: FormatBytes()

Post by hasantr » 20 Mar 2022, 14:35

haichen wrote:
20 Mar 2022, 09:07
Does this help?

Code: Select all

n:=9223372036854775807 ; B = 7,99 EB
;n:=200
x:= FormatBytes(n) 
m:= FormatStringToByte(x) 

tooltip, % n " B = " x " = " m " B" 
sleep,4000
return

FormatStringToByte(unitstrg){ 
    Units := {B:0, Bytes:0, KB:10, MB:20, GB:30,TB:40, PB:50, EB:60}
    RegExMatch(unitstrg, "im)(\d+,?\d{0,})\s{0,}(Bytes|KB|MB|GB|TB|PB|EB)",SubPat)
    SubPat1:=StrReplace(SubPat1,",",".")
    Return round(subPat1 * 2**Units[subPat2])
}

FormatBytes(N) { ; By SKAN on CT5H/D351 @ tiny.cc/formatbytes
Return DllCall("Shlwapi\StrFormatByteSize64A", "Int64",N, "Str",Format("{:16}",N), "Int",16, "AStr") 
}


Thanks. This is better. 8-)

Post Reply

Return to “Scripts and Functions (v1)”