#/usr/bin/env python # DesktopSearch.py - a script for searching Windows Desktop Search from the command line # # Usage: DesktopSearch.py [keyword...] # # This script requires the win32com package found at # http://sourceforge.net/projects/pywin32/. This means that you will # probably /not/ be able to get it to work with a Cygwin python -- use # the standard Python distribution for Windows instead. # # If you are running this under Emacs (as for use with anything.el), I # recommend that you install it in your PYTHONPATH (e.g., # c:/Python24/Lib/site-packages/), and call it as # 'python -m DesktopSearch '. anything-config.el will set # this up correctly for you if you have put the script in your # PYTHONPATH. # # Currently, this returns only plain files. It /should/ be possible # to provide usable links to Outlook messages and attachments in the # future. import win32com.client import sys, os, os.path # Evil init stuff reload(sys) sys.setdefaultencoding('utf-8') # The real work def search (*args): """Search""" connection = win32com.client.Dispatch("ADODB.Connection") recordset = win32com.client.Dispatch("ADODB.Recordset") connection.Open("Provider=Search.CollatorDSO;" "Extended Properties='Application=Windows';") query_base = ("SELECT System.FileName, System.ItemFolderPathDisplay " "FROM SYSTEMINDEX WHERE ") query_end = ("AND System.IsAttachment IS NULL " "ORDER BY System.DateAccessed DESC") name_clauses = [] contains_clauses = [] for arg in args: name_clauses.append("System.FileName Like '%%%s%%' " % arg) contains_clauses.append("Contains('\"%s\"') " % arg) query = (query_base + " AND ".join(name_clauses) + " OR (" + " AND ".join(contains_clauses) + ") ") #print query recordset.Open(query, connection) try: recordset.MoveFirst() except: return # nothing found while not recordset.EOF: if unicode(recordset.Fields.Item("System.FileName")) != u'None': sys.stdout.write(os.path.join( unicode(recordset.Fields.Item("System.ItemFolderPathDisplay")), unicode(recordset.Fields.Item("System.FileName")))) sys.stdout.write("\n") recordset.MoveNext() # Minimal argument parsing if __name__ == "__main__": if len(sys.argv) <= 1: sys.stderr.write("""Usage: DesktopSearch.py [keyword ...]\n""") sys.exit(1) apply(search, sys.argv[1:]) sys.exit(0)