/
search.py
51 lines (39 loc) · 1.08 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import pypff
import sys
if len(sys.argv) != 3:
print "Need to have 2 arguments: <pst file> <search term>"
sys.exit(1)
pst_file = sys.argv[1]
search_term = sys.argv[2]
print "PST file:", pst_file
print "Search term:", search_term
pst = pypff.file()
pst.open(pst_file)
print "Size:", pst.get_size()
print
msg_counter = 0
def search_dir(dir,path):
if dir.get_display_name():
new_path = path + u"/" + unicode(dir.get_display_name())
else:
new_path = path
print "Searching ", new_path
for i in range(0, dir.get_number_of_sub_messages()):
msg = dir.get_sub_message(i)
try:
if search_term in msg.get_plain_text_body():
write_to_file(msg)
except TypeError:
pass
for i in range(0, dir.get_number_of_sub_folders()):
search_dir(dir.get_sub_folder(i), new_path)
def write_to_file(msg):
global msg_counter
f = open("msgs/" + str(msg_counter) + ".txt","wb")
f.write("Subject: ")
f.write(msg.get_subject().encode("UTF-8"))
f.write("\n\n")
f.write(msg.get_plain_text_body().encode("UTF-8"))
f.close()
msg_counter = msg_counter + 1
search_dir(pst.get_root_folder(),u"")