Checking the size of user folders

(imported topic written by Lee Wei)

I helped someone with the following issue and spent some time creating a usable Manage Property. So I thought of sharing it.

The original problem statement and request:

"I have a question about scripts for checking the size of folders.

Outlook Express 6 has a big problem regarding the email folder size.

Once the size of its mail folder gets to a certain limit (I don’t remember the exact number), it would have a risk of crashing."

This is an interesting query that goes beyond getting the size of a folder.

NOTE: Please note that the query is resource intensive, and should not be run often. The evaluation interval should be set very long, for example, every 2 or 7 days.

The written relevance will do the following:

  1. First check to make sure that someone is logon to the computer, otherwise, it returns “No login user”.

  2. Verifies that Outlook Express is configured for the user, or return “No Outlook Express account”.

  3. Locate the folder where the Outlook Express files are located in the following registry, and return the size of the entire folder in MB.

(HKEY_CURRENT_USER\Identities{31391EF3-B3AC-4F12-94D8-DC2DA45E9526}\Software\Microsoft\Outlook Express\5.0\Store Root)

  1. If there are more than one Identity configured for Outlook Express, the Main (also call Default) identity will be used.

Here is the Relevance statement:

if exists (current user) then (if exists (value “Store Root” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it = name of current user) of key “HKEY_USERS” of registry) & “\Identities” & (Value “Default User ID” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it = name of current user) of key “HKEY_USERS” of registry) & “\Identities”) of registry as string) & “\Software\Microsoft\Outlook Express\5.0”) of registry) then (sum of sizes of descendants of folder(preceding text of last “” of ((Value “Local Settings” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it = name of current user) of key “HKEY_USERS” of registry) & “\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders”) of registry as string) & (following text of first “Settings” of (value “Store Root” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it = name of current user) of key “HKEY_USERS” of registry) & “\Identities” & (Value “Default User ID” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it = name of current user) of key “HKEY_USERS” of registry) & “\Identities”) of registry as string) & “\Software\Microsoft\Outlook Express\5.0”) of registry as string)))) / 1048576) as string & " MB" else “No Outlook Express account”) else “No login user”

Although the relevance looks very long, it is composed of smaller queries to resolve specific information we need.

Issue 1: When the BES Client runs on a computer, it runs as the SYSTEM account, and the HKEY_CURRENT_USER hive is different than the actual login user. We can use the following relevance to dynamically return the appropriate hive from HKEY_USERS instead.

Q: (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it = name of current user) of key “HKEY_USERS” of registry)

A: S-1-5-21-1606980848-1767777339-682003330-2286

Issue 2: An end-user can setup multiple Identities for his/her Outlook Express application. Each identity will have a different folder of files. The query is currently written to return only the disk usage for the Main Identity. This is done with looking at the “HKEY_CURRENT_USER\Identities\Default User ID” registry key. The relevance can be modified to report the folder size of the “Last User ID” by using the identity key at “HKEY_CURRENT_USER\Identities\Last User ID”.

Q: (Value “Default User ID” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it = name of current user) of key “HKEY_USERS” of registry) & “\Identities”) of registry as string)

A: {31391EF3-B3AC-4F12-94D8-DC2DA45E9526}

Issue 3: The Outlook Express folder name located at registry key “HKEY_CURRENT_USER\Identities{31391EF3-B3AC-4F12-94D8-DC2DA45E9526}\Software\Microsoft\Outlook Express\5.0\Store Root” uses an environment variable %25UserProfile%25 that needs to be expanded appropriately to retrieve a usable directory name.

Q: (Value “Local Settings” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it = name of current user) of key “HKEY_USERS” of registry) & “\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders”) of registry as string)

A: C:\Documents and Settings\lee_wei\Local Settings

Issue 4: How do I calculate the size of a folder and return the size in MB. This is original question and the key is using the “descendants of folders” object.

Q: (sum of sizes of descendants of folder “C:\Documents and Settings\lee_wei\Local Settings\Application Data\Identities{31391EF3-B3AC-4F12-94D8-DC2DA45E9526}\Microsoft\Outlook Express” / 1048576) as string & " MB"

A: 0 MB

(imported comment written by brolly3391)

Lee,

I was trying this relevance out and ran into a small issue. On our ancient NT4.0 domain, the result from

name of current user

is a mixed case string and the result from the registry call is a lowercase registry key, so the comparision was failing and I was getting

E: Singular expression refers to nonexistent object.

for your main relevance and examples 1-3.

q: values “Logon User Name” of keys “Explorer” of keys “CurrentVersion” of keys “Windows” of keys “Microsoft” of keys “Software” of keys of key “HKEY_USERS” of registry

A: brolly

A: administrator

T: 1.510 ms

I: plural registry key value

q: name of current user

A: Brolly

T: 1.215 ms

I: singular string

I replaced

of it = name of current user

with

of it as string as lowercase = name of current user as lowercase

giving an slightly more error resistant relevance statement that works in my environment.

q: if exists (current user) then (if exists (value “Store Root” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it as string as lowercase = name of current user as lowercase) of key “HKEY_USERS” of registry) & “\Identities” & (Value “Default User ID” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it as string as lowercase = name of current user as lowercase) of key “HKEY_USERS” of registry) & “\Identities”) of registry as string) & “\Software\Microsoft\Outlook Express\5.0”) of registry) then (sum of sizes of descendants of folder(preceding text of last “” of ((Value “Local Settings” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it as string as lowercase = name of current user as lowercase) of key “HKEY_USERS” of registry) & “\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders”) of registry as string) & (following text of first “Settings” of (value “Store Root” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it as string as lowercase = name of current user as lowercase) of key “HKEY_USERS” of registry) & “\Identities” & (Value “Default User ID” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it as string as lowercase = name of current user as lowercase) of key “HKEY_USERS” of registry) & “\Identities”) of registry as string) & “\Software\Microsoft\Outlook Express\5.0”) of registry as string)))) / 1048576) as string & " MB" else “No Outlook Express account”) else “No login user”

A: 0 MB

T: 25.450 ms

I: singular string

It’s a very handy property. Thanks for sharing it.

An interesting side note, I noticed that if I compressed the reference to the registry key from

of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it

to

of key “Software\Microsoft\Windows\CurrentVersion\Explorer” of it

that I realized a 20% improvement in evaluation time for that segment of relevance. Why that happens is a conversation for another thread I think.

Cheers,

Brolly.

(imported comment written by ErnieF91)

We are evaluating redirecting the local “My Documents” folder to network storage for a new retension policy. I modified this relevance to return the size of the logged on users My Documents folder.

If exists (current user) then

if exists (Value “Personal” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it = name of current user) of key “HKEY_USERS” of registry) & “\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders”) of registry ) then

sum of sizes of descendants of folder (Value “Personal” of Key (“HKEY_USERS” & (name of key whose (value “Logon User Name” of key “Explorer” of key “CurrentVersion” of key “Windows” of key “Microsoft” of key “Software” of it = name of current user) of key “HKEY_USERS” of registry) & “\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders”) of registry as string) as string else “No My Document folder”

else “No login user”

I tried Brolly’s compression of the registry key and it was slower, so I left it expanded.

Ernie