Python Search Toolbox for Windows

1. MySearch-3.py — Show first 3 matches per file

Purpose: See the first 3 lines in each file containing the search text.

import sys
import os
import fnmatch

if len(sys.argv) != 4:
    print(f"Usage: {os.path.basename(sys.argv[0])} "search text" filemask rootfolder")
    sys.exit(1)

search_text = sys.argv[1].lower()
filemask = sys.argv[2]
root_folder = sys.argv[3]

for folder_name in os.listdir(root_folder):
    folder_path = os.path.join(root_folder, folder_name)
    if not os.path.isdir(folder_path) or folder_name.startswith("_"):
        continue

    print(f"\n=== Searching {folder_path} ===")

    for file_name in os.listdir(folder_path):
        if file_name.startswith("_"):
            continue
        if not fnmatch.fnmatch(file_name, filemask):
            continue

        file_path = os.path.join(folder_path, file_name)
        match_count = 0

        for encoding in ("utf-8", "cp1252"):
            try:
                with open(file_path, encoding=encoding) as f:
                    for lineno, line in enumerate(f, start=1):
                        if search_text in line.lower():
                            print(f"{lineno}:{line.rstrip()}")
                            match_count += 1
                            if match_count >= 3:
                                raise StopIteration
                break
            except (UnicodeDecodeError, StopIteration):
                break
            except Exception as e:
                print(f"Could not read file {file_path}: {e}")
                break

2. MySearch_List.py — List files containing a match

Purpose: Only list files containing the search text so you can open them in an editor.

import sys
import os
import fnmatch

if len(sys.argv) != 4:
    print(f"Usage: {os.path.basename(sys.argv[0])} "search text" filemask rootfolder")
    sys.exit(1)

search_text = sys.argv[1].lower()
filemask = sys.argv[2]
root_folder = sys.argv[3]

for folder_name in os.listdir(root_folder):
    folder_path = os.path.join(root_folder, folder_name)
    if not os.path.isdir(folder_path) or folder_name.startswith("_"):
        continue

    for file_name in os.listdir(folder_path):
        if file_name.startswith("_"):
            continue
        if not fnmatch.fnmatch(file_name, filemask):
            continue

        file_path = os.path.join(folder_path, file_name)

        for encoding in ("utf-8", "cp1252"):
            try:
                with open(file_path, encoding=encoding) as f:
                    for line in f:
                        if search_text in line.lower():
                            print(file_path)
                            raise StopIteration
                break
            except (UnicodeDecodeError, StopIteration):
                break
            except Exception as e:
                print(f"Could not read file {file_path}: {e}")
                break

3. Optional .bat wrapper for Windows

Purpose: Run Python scripts like native commands.

@echo off
python "%~dp0MySearch_List.py" %*

or for MySearch-3.py:

@echo off
python "%~dp0MySearch-3.py" %*

Now you can run from CMD:

MySearch_List "tutor" "*.html" "W:\wamp\www\carriersnc"
MySearch-3 "tutor" "*.html" "W:\wamp\www\carriersnc"

Workflow Summary

  1. Use MySearch_List.py to get a list of files containing your text
  2. Open the file(s) in your favorite editor
  3. Optionally use MySearch-3.py if you want a preview of the first 3 hits per file
  4. All scripts handle UTF-8 and Windows-1252 files gracefully
  5. Skip _ folders/files automatically

Conclusion

This setup is fast, reliable, and future-proof. You now have a professional Python-based search workflow that fully replaces your previous batch/PowerShell scripts.