File size calculation (as in Windows)

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
andy_BSZY
Posts: 67
Joined: 13 Sep 2023, 09:52

File size calculation (as in Windows)

06 Oct 2023, 05:52

I found the topic

I would like the script to display the size of the file in a format like this presented by Windows, for example:

5,09 KB
6,21 MB
5,43 GB

How to use this function for example with the FileGetSize option

Code: Select all

Size := FileGetSize("C:\My Documents\test.doc")
I would like to be able to calculate the size of the content of the whole folder and send the result to a text file. Unfortunately I can't benefit from the information that is in the topic whose link I posted (lack of knowledge), can you write me how such a script should look like?
User avatar
andymbody
Posts: 906
Joined: 02 Jul 2017, 23:47

Re: File size calculation (as in Windows)

06 Oct 2023, 06:18

Here is a combination of the two.

Code: Select all

#Requires AutohotKey v2.0+
#SingleInstance force

FolderSize := 0
WhichFolder := DirSelect()  ; Ask the user to pick a folder.
Loop Files, WhichFolder "\*.*", "R"
    FolderSize += A_LoopFileSize

FormattedSize := FormatBytes(FolderSize)	; send this to file

MsgBox "Size of " WhichFolder " is " FormattedSize

FormatBytes(N)
{
; By SKAN on CT5H/D351 @ tiny.cc/formatbytes
	return DllCall("Shlwapi\StrFormatByteSize64A", "Int64",N, "Str",Format("{:16}",N), "Int",16, "AStr")
}
RussF
Posts: 1269
Joined: 05 Aug 2021, 06:36

Re: File size calculation (as in Windows)

06 Oct 2023, 06:28

Since this is the V2 forum, perhaps you should use @SKAN's V2 version:

Code: Select all

#Requires AutoHotkey v2.0

MsgBox FormatBytes(0x7FFFFFFFFFFFFFFF) ; 7.99 EB

FormatBytes(N) ; By SKAN for ah2 on CT5H/D68S @ autohotkey.com/r?t=3567
{
    Return DllCall("Shlwapi\StrFormatByteSizeW", "int64",N, "str",Format("{:16}",""), "int",16, "str")
}
Russ
User avatar
WarlordAkamu67
Posts: 220
Joined: 21 Mar 2023, 06:52

Re: File size calculation (as in Windows)

06 Oct 2023, 06:30

You don't need to use the FileGetSize as loops have built in variables that can used. This allows you to select a folder. It will recuse into sub folders. It will count the number of files and add up all the bytes. Follow the link to FileLoops. https://www.autohotkey.com/docs/v2/Variables.htm#loop

Code: Select all

#Requires AutoHotkey v2.0

totalByte := 0
theCount := 0

folderSelection := DirSelect()
Loop Files (folderSelection "\*"), ("R") {
  totalByte += A_LoopFileSize
  theCount++
}

MsgBox(theCount " total files. " totalByte " bytes")
User avatar
andymbody
Posts: 906
Joined: 02 Jul 2017, 23:47

Re: File size calculation (as in Windows)

06 Oct 2023, 06:36

RussF wrote:
06 Oct 2023, 06:28
Since this is the V2 forum, perhaps you should use @SKAN's V2 version:
Wasn't aware of that one... thanks!
User avatar
andy_BSZY
Posts: 67
Joined: 13 Sep 2023, 09:52

Re: File size calculation (as in Windows)

06 Oct 2023, 06:57

Great :D , thanks for your help. Could you please also write a version with, if I want to have a source directory defined (e.g. C: Windows\test) without selecting it with the Select option. Please also add the possibility to export the result to a text file.
User avatar
andymbody
Posts: 906
Joined: 02 Jul 2017, 23:47

Re: File size calculation (as in Windows)

06 Oct 2023, 07:01

andy_BSZY wrote:
06 Oct 2023, 06:57
Could you please also write a version with, if I want to have a source directory defined (e.g. C: Windows\test) without selecting it with the Select option. Please also add the possibility to export the result to a text file.
I think you should put in some effort, don't you?
User avatar
andy_BSZY
Posts: 67
Joined: 13 Sep 2023, 09:52

Re: File size calculation (as in Windows)

06 Oct 2023, 07:15

andymbody wrote:
06 Oct 2023, 07:01
I think you should put in some effort, don't you?
You're right, I already did, thanks for your help. Have a great weekend.
User avatar
andymbody
Posts: 906
Joined: 02 Jul 2017, 23:47

Re: File size calculation (as in Windows)

06 Oct 2023, 07:23

andy_BSZY wrote:
06 Oct 2023, 07:15
I already did
Sorry... I didn't see the code, if it was posted.

From my perspective, a fish was provide and the response was... great... now please clean it, cook it, and serve it to me while you wait for my next request?

I'm headed to work, so perhaps you will receive this second request from one of the other members.

Have a good weekend also.
User avatar
andy_BSZY
Posts: 67
Joined: 13 Sep 2023, 09:52

Re: File size calculation (as in Windows)

06 Oct 2023, 08:37

andymbody wrote:
06 Oct 2023, 07:23
Sorry... I didn't see the code, if it was posted.

From my perspective, a fish was provide and the response was... great... now please clean it, cook it, and serve it to me while you wait for my next request?

I'm headed to work, so perhaps you will receive this second request from one of the other members.
I have modified your code and used the code from @boiler here. I have changed a bit, the main thing is that I already know how it works. Here you go, this is what it looks like.

Code: Select all

#Requires AutohotKey v2.0+
#SingleInstance force

FolderSize := 0
WhichFolder := "D:\Programs"  ; Ask the user to pick a folder.
Loop Files, WhichFolder "\*.*", "R"
    FolderSize += A_LoopFileSize

FormattedSize := FormatBytes(FolderSize)	; send this to file

; MsgBox "Size of " WhichFolder " is " FormattedSize   ; Optionally

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

TimeFormat := FormatTime(, "yyyy.MM.dd HH.mm")

Some_Var1 := FormatBytes(FolderSize)
Some_Var2 := "Info2"


WriteLog("Folder size " Some_Var1 " and " Some_Var2)

WriteLog(text) {
	FileAppend TimeFormat ": " text "`n", 'logfile.txt' ; can provide a full path to write to another directory
}

Entry in log file

Code: Select all

2023.10.06 15.18: Folder size 62,9 GB and Info2
RussF
Posts: 1269
Joined: 05 Aug 2021, 06:36

Re: File size calculation (as in Windows)

06 Oct 2023, 08:58

Since TimeFormat is a variable, you should not have single quotes around it in your FileAppend statement. In fact, I can't see how how you get the output that you claim.

Russ
User avatar
andy_BSZY
Posts: 67
Joined: 13 Sep 2023, 09:52

Re: File size calculation (as in Windows)

06 Oct 2023, 10:12

RussF wrote:
06 Oct 2023, 08:58
Since TimeFormat is a variable, you should not have single quotes around it in your FileAppend statement. In fact, I can't see how how you get the output that you claim.

Russ
Of course you are right, I just pasted the code of the non-final version. Corrected.
teadrinker
Posts: 4335
Joined: 29 Mar 2015, 09:41
Contact:

Re: File size calculation (as in Windows)

06 Oct 2023, 11:53

andy_BSZY wrote:

Code: Select all

DllCall("Shlwapi\StrFormatByteSize64A", "Int64",N, "Str",Format("{:16}",N), "Int",16, "AStr")
Quite a weird way to use this function. It's better just like this:

Code: Select all

MsgBox DllCall("Shlwapi\StrFormatByteSize", "Int64", 1234567890, "Ptr", Buffer(16), "Int", 16, "Str")
User avatar
andy_BSZY
Posts: 67
Joined: 13 Sep 2023, 09:52

Re: File size calculation (as in Windows)

06 Oct 2023, 12:21

teadrinker wrote:
06 Oct 2023, 11:53
andy_BSZY wrote:

Code: Select all

DllCall("Shlwapi\StrFormatByteSize64A", "Int64",N, "Str",Format("{:16}",N), "Int",16, "AStr")
Quite a weird way to use this function. It's better just like this:

Code: Select all

MsgBox DllCall("Shlwapi\StrFormatByteSize", "Int64", 1234567890, "Ptr", Buffer(16), "Int", 16, "Str")
Perfect, thank you very much :bravo:
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: File size calculation (as in Windows)

10 Oct 2023, 22:00

teadrinker wrote:
06 Oct 2023, 11:53
andy_BSZY wrote:

Code: Select all

DllCall("Shlwapi\StrFormatByteSize64A", "Int64",N, "Str",Format("{:16}",N), "Int",16, "AStr")
Quite a weird way to use this function. It's better just like this:

Code: Select all

MsgBox DllCall("Shlwapi\StrFormatByteSize", "Int64", 1234567890, "Ptr", Buffer(16), "Int", 16, "Str")
 
Buffer(32) would be correct.

Code: Select all

MsgBox DllCall("Shlwapi\StrFormatByteSizeW", "int64",1234567890, "ptr",Buffer(32), "int",16, "str")

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: jaccotjuhhh, vmech and 30 guests