Compare size of two folders

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Oldboy
Posts: 19
Joined: 09 Aug 2018, 03:03

Compare size of two folders

Post by Oldboy » 28 Jul 2021, 02:24

Hi, I need to compare the size of two folders, i found this script that works:

Code: Select all

bytes := ComObjCreate("Scripting.FileSystemObject").GetFolder("C:\TEST 1").Size
bytes2 := ComObjCreate("Scripting.FileSystemObject").GetFolder("C:\TEST 2").Size
if bytes = %bytes2%
{
msgbox, OK
}
else
{
msgbox, NO
}
Return
But I need also to insert a variable for the directory like this:

Code: Select all

var := "TEST 1"
bytes := ComObjCreate("Scripting.FileSystemObject").GetFolder("C:\%var%").Size
bytes2 := ComObjCreate("Scripting.FileSystemObject").GetFolder("C:\TEST 2").Size
if bytes = %bytes2%
{
msgbox, OK
}
else
{
msgbox, NO
}
Return
[Mod edit: [code][/code] tags added.]

And this not work because the %var% is betwenn the quotation marks.
Any help?
Thanks
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Compare size of two folders

Post by Xtra » 28 Jul 2021, 03:26

Code: Select all

var1 := "C:\TEST 1"
var2 := "C:\TEST 2"
bytes1 := ComObjCreate("Scripting.FileSystemObject").GetFolder(var1).Size
bytes2 := ComObjCreate("Scripting.FileSystemObject").GetFolder(var2).Size
if (bytes1 = bytes2)
{
    msgbox, OK
}
else
{
    msgbox, NO
}
Return
User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: Compare size of two folders

Post by boiler » 28 Jul 2021, 05:09

And if you want the "C:\" separated as in your example:

Code: Select all

var := "TEST 1"
bytes := ComObjCreate("Scripting.FileSystemObject").GetFolder("C:\" . var).Size

See the dot ("concatenate") operator in Expression Operators.
Oldboy
Posts: 19
Joined: 09 Aug 2018, 03:03

Re: Compare size of two folders

Post by Oldboy » 29 Jul 2021, 08:28

boiler wrote:
28 Jul 2021, 05:09
And if you want the "C:\" separated as in your example:

Code: Select all

var := "TEST 1"
bytes := ComObjCreate("Scripting.FileSystemObject").GetFolder("C:\" . var).Size

See the dot ("concatenate") operator in Expression Operators.
Thanks a lot, the directory now Work!
The only problem is that the result is always "NO" even if there are the same files in any folder...
I don't know why but works only with:

Code: Select all

IfEqual, bytes1, %bytes2%
instead of:

Code: Select all

if (bytes1 = bytes2)
or

Code: Select all

if (bytes1 = %bytes2%)
User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: Compare size of two folders

Post by boiler » 29 Jul 2021, 09:33

The line below is always wrong (except when purposely double-dereferencing, which you are definitely not doing here):

Code: Select all

if (bytes1 = %bytes2%)
The other two produce the same result. You must have had something else wrong when you say the one with the parentheses didn't work. Here's a demonstration that the two produce the same result:

Code: Select all

bytes1 := 23456
bytes2 := 23456

IfEqual, bytes1, %bytes2%
	MsgBox, They are equal when compared using IfEqual.
else
	MsgBox, They are NOT equal when compared using IfEqual.

if (bytes1 = bytes2)
	MsgBox, They are equal when compared using if ( ).
else
	MsgBox, They are NOT equal when compared using if ( ).

Make sure you are consistent in whether you use bytes vs. bytes1 and var vs. var1 because the various code snippets in this thread have sometimes used one and sometimes the other.

Also note that if (bytes1 = bytes2) is not the same as if bytes1 = bytes2. The latter would need to be if bytes1 = %bytes2%. That is because the former is the expression version of if, while the latter is the legacy version of if (as is IfEqual). The legacy versions have been deprecated and should no longer be used.
Post Reply

Return to “Ask for Help (v1)”