Created
November 16, 2015 23:04
-
-
Save mprz/236bf88bffd8c46c8fc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import os, sys, msvcrt, webbrowser | |
# first arg = location, i.e. c:\myfiles | |
# second arg = file mask, i.e. *.py | |
# third arg and so on = strings to find | |
args = sys.argv | |
if len(args) < 4: | |
print("Usage:") | |
print("SEEK location filemask string1 [string2 string3 string4 ...]") | |
print("SEEK c:\\recipes *.txt tomatoes onions leeks") | |
exit("Terminated.") | |
location = args[1] | |
extension = os.path.splitext(args[2])[1] | |
items = [] | |
stuff = "" | |
output_file_name = "seek.txt" | |
html_file = "seek.html" | |
for x in range (3, len(args)): | |
items.append(args[x]) | |
stuff = stuff + args[x] + " " | |
print ("Searching location %s, filemask is %s " %(location, extension)) | |
print ("Looking for: [%s]" %stuff) | |
def search_text_file(fname, content): | |
num_of_matches = len(content) | |
if num_of_matches == 0: | |
return True | |
item_x_found = [False] * num_of_matches | |
data = file(fname) | |
for line in data: | |
for item_num in range(num_of_matches): | |
if content[item_num] in line: | |
item_x_found[item_num] = True | |
if False in item_x_found: | |
return False | |
return True | |
def create_html(fname): | |
input_data = file(output_file_name) | |
output_file = open(fname, "w") | |
output_file.write("<body>\n") | |
for line in input_data: | |
line=line.replace("\n", "") | |
output_file.write("<a href=\"%s\">%s</a><br />\n" %(line, line)) | |
output_file.write("</body>") | |
output_file.close() | |
def search_files(): | |
items_found, files_found, files_checked = 0, 0, 0 | |
output_file = open(output_file_name, "w") | |
print ("Enumerating folders...") | |
for dirname, dirnames, filenames in os.walk(location): | |
print ("Searched %s files, matched %s, of which %s contain all search terms. " %(files_found, files_checked, items_found), end='\r') | |
for filename in filenames: | |
files_found +=1 | |
if msvcrt.kbhit(): | |
output_file.close() | |
print ('\n\nKey pressed, search stopped') | |
return items_found | |
currentfile = os.path.join(dirname, filename) | |
if filename.endswith(extension): | |
files_checked +=1 | |
if search_text_file(currentfile, items): | |
output_file.write(currentfile) | |
output_file.write('\n') | |
items_found +=1 | |
print() | |
output_file.close() | |
create_html(html_file) | |
return items_found | |
print ("Total items found: %s" %search_files()) | |
ie = webbrowser.get(webbrowser.iexplore) | |
ie.open(os.path.abspath(html_file)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment