Format a number to its Byte Size

#1
Converts a numeric value into a string that represents the number expressed as a size value in bytes, kilobytes, megabytes, or gigabytes, depending on the size.

Code:
ver

TCC  23.00.28 x64   Windows 7 [Version 6.1.7601]
Code:
function StrFormatByteSize=`%@winapi[shlwapi.dll,StrFormatByteSize,%1,BUFFER,256]`
Code:
echo %@StrFormatByteSize[1000]
1000 bytes

echo %@StrFormatByteSize[1023]
1023 bytes

echo %@StrFormatByteSize[1024]
1.00 KB

echo %@StrFormatByteSize[10240]
10.0 KB

echo %@StrFormatByteSize[102400]
100 KB

echo %@StrFormatByteSize[1024000]
0.97 MB

echo %@StrFormatByteSize[2400000000]
2.23 GB
Joe
 
Likes: Charles G
#2
Hi Charles;

I tried this with TCC 18.00.18 on Windows 7, with no success. Since it's the same shlwapi.dll, it should not be dependent on the version of TCC.

What I get is:

Code:
TCC  18.00.18   Windows 7 [Version 6.1.7601]

function StrFormatByteSize=`%@winapi[shlwapi.dll,StrFormatByteSize,%1,BUFFER,256]`

echo %@StrFormatByteSize[1000]
TCC: (Sys) The operation completed successfully.
 "StrFormatByteSize"
Attempting this directly, not through a function, I see the same:

Code:
echo %@winapi[shlwapi.dll, StrFormatByteSize, 1000, BUFFER, 256]

TCC: (Sys) The operation completed successfully.
 "StrFormatByteSize"
Is there some other dependency that you could think of why this would not work?
 
#4
Hi Joe;

Thanks for the link. I admit I'm not familiar with shlwapi.dll. I did some reading yesterday, but I didn't see anything obvious that would explain why it didn't work.

I posted yesterday from my work PC, which I can't check that version now; I'll check it later today.

On my two home PCs, my Win10 box only has TCC/LE 14, which doesn't have the winapi function, so I can't test that, but my Win7 machine shows 6.1.7601.17514. I checked, and there are four instances of the dll (C:\Windows\System32, C:\Windows\SysWOW64, and two in the winsxs directories), but all four are at the same patch level.

For whatever it's worth, the SHELL32.dll is 6.1.7601.24234.

The interesting thing is that shell32.dll is dated 2018-08-13, while shlwapi.dll is 2010-11-20, almost 8 years old. I've got a security update pending for November 2018, but other than that, the OS should be at the current patch level (at least according to Windows Update). And I can't imagine that for StrFormatByteSize, it matters whether it's Win7 Pro. Home, or another edition.
 
#5
I tried on a Windows 7 32-bit system with TCC 23, and found that the version of shlwapi.dll is...
1542375161197.png

The following returns a different error message than what you received,

Code:
[C:\utils]function StrFormatByteSize=`%@winapi[shlwapi.dll,StrFormatByteSize,%1,BUFFER,256]`

[C:\utils]echo %@StrFormatByteSize[1000]
TCC: (Sys) The specified procedure could not be found.
 "StrFormatByteSize"
Joe