Question

I’ve been using redemption dll Redemption to try and count message items in a pst file. My lack of python knowledge means what I’m currently doing has potential to miss items entirely. It seems, that to iterate over folder items in the hierarchy in a pst file, you have to enumerate the folder count first via a redemption/Microsoft folder.count. Once I have the count number, then I descend into each folder to count the items in that folder. However, the nested sub folder structure is variable, and could be 5, 10 or more folders deep in each folder and I wouldn't know where to stop.

Inbox
 Personal
  Dad
  Mum
Work
Sent items
  Archive
  Old Mail
Outbox
Junk Items

I’ve been using a for loop; something like this to count the sub-folders and providing the count is not 0, descending to count the folder items

If folder is not 0:
  for folder in range(root.folders.count):
  folder+=1
  messageITEMS=root.folders(folder).item.count 

but this could mean I miss a sub folder somewhere in the pst hierarchy. I need to continue the loop, dropping from folder to folder, ‘till the sub folder count = 0. I’ve read over Itertools product, but my lack of python knowledge is stopping me from seeing if this is the correct answer or how I may use it to achieve what I need to; which is to make sure I've counted all mail items in every folder in the pst. Any help is really appreciated. Thanks

Was it helpful?

Solution

Sounds like you want to add up the number of messages in all directories and subdirectories. The folder directory is effectively a tree -- one folder can contain many branching folders. Iterating through a tree is best done through recursion.

def count_messages(folder):
    messages = folder.item.count
    for sub_folder in folder.folders:
        messages += count_messages(sub_folder)
    return messages

total_messages = count_messages(root)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top