import os
import tkinter as tk
from tkinter import ttk, messagebox
from tkinter import filedialog
class FileSearchApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("Najeeb Advanced File/Folder Searcher")
self.geometry("900x600")
self.configure(bg="#2e3f4f") # Background color
# Variables
self.search_query = tk.StringVar()
self.selected_drive = tk.StringVar(value="Select All Drives")
self.detected_drives = self.get_drives()
# Create widgets
self.create_widgets()
def get_drives(self):
"""Detect all available drives on the system."""
drives = []
for drive in range(ord('A'), ord('Z') + 1):
drive_letter = f"{chr(drive)}:"
if os.path.exists(drive_letter):
drives.append(drive_letter)
return ["Select All Drives"] + drives
def create_widgets(self):
# Drive Selection Frame
drive_frame = tk.Frame(self, bg="#2e3f4f")
drive_frame.pack(pady=10)
# Drive Dropdown
tk.Label(drive_frame, text="Select Drive:", fg="white", bg="#2e3f4f", font=("Times New Roman", 14)).pack(side=tk.LEFT, padx=5)
self.drive_dropdown = ttk.Combobox(drive_frame, textvariable=self.selected_drive, values=self.detected_drives, state="readonly", width=20)
self.drive_dropdown.pack(side=tk.LEFT, padx=5)
# Search Field
#search_frame = tk.Frame(self, bg="#2e3f4f")
#search_frame.pack(pady=10)
tk.Label(drive_frame, text="Enter File/Folder Name:", fg="white", bg="#2e3f4f", font=("Times New Roman", 14)).pack(side=tk.LEFT, padx=5)
self.search_entry = tk.Entry(drive_frame, textvariable=self.search_query, width=32, font=("Times New Roman", 14))
self.search_entry.pack(side=tk.LEFT, padx=5)
# Buttons (Search, Open, Save Results, Clear) in one line
button_frame = tk.Frame(self, bg="#2e3f4f")
button_frame.pack(pady=10)
button_style = {"bg": "#007acc", "fg": "white", "font": ("Times New Roman", 14), "width": 17, "relief": tk.RAISED}
self.search_button = tk.Button(button_frame, text="Search Files/Folders", command=self.search_files, **button_style)
self.search_button.pack(side=tk.LEFT, padx=5)
self.open_file_button = tk.Button(button_frame, text="Open Selected Item", command=self.open_selected_file, **button_style)
self.open_file_button.pack(side=tk.LEFT, padx=5)
self.save_button = tk.Button(button_frame, text="Save Results", command=self.save_results, **button_style)
self.save_button.pack(side=tk.LEFT, padx=5)
self.clear_button = tk.Button(button_frame, text="Clear Results", command=self.clear_results, **button_style)
self.clear_button.pack(side=tk.LEFT, padx=5)
# File Listbox
self.result_frame = tk.Frame(self, bg="#2e3f4f")
self.result_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
self.file_listbox = tk.Listbox(self.result_frame, selectmode=tk.SINGLE, width=100, height=25, bg="#1c2833", fg="white", font=("Courier New", 10))
self.file_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Scrollbars
scrollbar_vertical = tk.Scrollbar(self.result_frame, orient="vertical", command=self.file_listbox.yview)
self.file_listbox.config(yscrollcommand=scrollbar_vertical.set)
scrollbar_vertical.pack(side=tk.RIGHT, fill="y")
def search_files(self):
"""Search files and folders based on the search query."""
self.file_listbox.delete(0, tk.END) # Clear previous results
query = self.search_query.get().strip().lower()
if not query:
messagebox.showwarning("Input Required", "Please enter a file or folder name to search.")
return
selected_drive = self.selected_drive.get()
drives_to_search = self.detected_drives[1:] if selected_drive == "Select All Drives" else [selected_drive]
matching_items = []
# Traverse drives and collect matching files and folders
for drive in drives_to_search:
if os.path.exists(drive):
for root, dirs, files in os.walk(drive):
# Include hidden files and folders
items = dirs + files
for item in items:
if query in item.lower():
item_path = os.path.join(root, item)
matching_items.append(item_path)
self.file_listbox.insert(tk.END, item_path)
self.update_idletasks() # Keep GUI responsive
# Show a message if no items are found
if not matching_items:
messagebox.showinfo("Search Complete", f"No files or folders found containing '{query}' in {selected_drive}.")
def open_selected_file(self):
"""Open the selected file or folder with the default program."""
selected_item_index = self.file_listbox.curselection()
if not selected_item_index:
messagebox.showwarning("No Selection", "Please select a file or folder to open.")
return
selected_item = self.file_listbox.get(selected_item_index[0])
try:
os.startfile(selected_item) # Open with default program
except Exception as e:
messagebox.showerror("Error Opening Item", str(e))
def save_results(self):
"""Save the search results to a file."""
if self.file_listbox.size() == 0:
messagebox.showwarning("No Results", "No results to save.")
return
file_path = filedialog.asksaveasfilename(
defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
title="Save Results As"
)
if not file_path:
return
try:
with open(file_path, "w", encoding="utf-8") as file:
for item in self.file_listbox.get(0, tk.END):
file.write(item + "\n")
messagebox.showinfo("Success", f"Results saved to {file_path}")
except Exception as e:
messagebox.showerror("Error Saving Results", str(e))
def clear_results(self):
"""Clear the results from the Listbox."""
self.file_listbox.delete(0, tk.END)
if __name__ == "__main__":
app = FileSearchApp()
app.mainloop()