Powered by Python: Easy File Finder

More often than not I dig into tens of gigas of files to search for one I vaguely remember. I had though I could have shortcuts to files I frequent according to keywords.

Here comes the solution implemented with Python. This script take a criterion substring and searching all file names, whenever one is matched,  a corresponding shortcut is created into a designated folder.

  1: from os import walk
  2: from os.path import join, getsize
  3: import datetime
  4: import sys
  5: import os
  6: import re
  7: import win32com.client
  8: 
  9: def CreateShortcut(WhereSrc, WhereShortcut, ShortcutName):
 10:     """ This function creates a Windows shortcut for a directory or file,
 11:         WhereSrc:      path to a directory or file
 12:         WhereShortcut: path to the shortcut being placed
 13:         ShortcutName:  name of the Shortcut
 14:         Copyright @ Tomgee, 2007
 15:     """
 16: 
 17:     shell = win32com.client.Dispatch("WScript.Shell")
 18:     shortcut = shell.CreateShortCut(WhereShortcut + "\\" + ShortcutName + ".lnk")
 19:     shortcut.Targetpath = WhereSrc
 20:     shortcut.save()
 21: 
 22: def GoFind(path, substr, WhereShortcut):
 23:     """ this function take a valid directory path as input
 24:         walk through all files and directories in the passed in path, 
 25:         create a shortcut for each matched item.
 26:         path:           path to a directory or file
 27:         WhereShortcut:  path to the shortcut being placed
 28:         substr:         criteria string to match
 29:         Copyright @ Tomgee, 2007
 30:     """
 31: 
 32:     logfile = WhereShortcut + r'\log.txt'
 33:     print "Going through %s to find \'%s\'(case insensitive), \nsubdirectory included..."%(path, substr)
 34:     print "a log will be saved to %s"%logfile
 35:     f = open(logfile, 'a') # open a log file
 36:     sys.stdout = f   # un-comment this line out if the output to a file is preferred 
 37: 
 38:     now = datetime.datetime.now()
 39:     print "\n***********************************************"
 40:     print "Timestamp: %s"%now.strftime("%y-%m-%d, %H:%M:%S")
 41: 
 42:     totalCount = 0
 43:     substr_upper = substr.upper()
 44:     for root,dirs, files in walk(path):
 45:         for file in files:
 46:             if substr_upper in file.upper():
 47:                 fullpath = join(root,file)
 48:                 CreateShortcut(fullpath, WhereShortcut, file)
 49:                 print "File #%d: %s"%(totalCount,fullpath)
 50:             totalCount += 1
 51:     print "Totally %d files/directories under %s have been searched"%(totalCount, path)
 52:     print r'********* The End *********'
 53: 
 54: def main():
 55:     argCount            = len(sys.argv)
 56:     argCurDir           = os.getcwd()
 57:     argMatchStr         = ""
 58:     argShortcutsDir     = os.getcwd() + r'\shortcuts_for_'
 59: 
 60:     print "usage: %s [StringToMatch] [DirectoryToSearch, e.g. c:\] [DirectoryToPutShortcuts, e.g. c:\]" %os.path.basename(sys.argv[0])
 61:     if argCount > 1:
 62:         argMatchStr = sys.argv[1]
 63:     if argCount > 2:
 64:         argCurDir = sys.argv[2]
 65:     if argCount > 3:
 66:         argShortcutsDir = sys.argv[3]
 67: 
 68:     argFinalShortcutsDir = argShortcutsDir + argMatchStr
 69:     if not os.path.lexists(argFinalShortcutsDir):
 70:         os.mkdir(argFinalShortcutsDir)
 71:     GoFind(argCurDir, argMatchStr, argFinalShortcutsDir)
 72: if __name__ == '__main__': main()
 73: 
 74: 
Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: