Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 Extensions Search")
- self.geometry("900x600")
- self.configure(bg="#2e3f4f") # Background color
- # Variables
- self.selected_extension = tk.StringVar(value=".exe")
- self.custom_extension = 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)
- # Extension Selection Frame
- #extension_frame = tk.Frame(self, bg="#2e3f4f")
- #extension_frame.pack(pady=10)
- # Dropdown Menu for Extensions
- tk.Label(drive_frame, text="Select Extension:", fg="white", bg="#2e3f4f", font=("Times New Roman", 14)).pack(side=tk.LEFT, padx=5)
- self.extension_dropdown = ttk.Combobox(drive_frame, textvariable=self.selected_extension, values=[
- ".exe", ".txt", ".bat", ".ini", ".reg", ".ahk", ".au3", ".m3u", ".m3u8", ".py", ".pyw", ".vbs", ".cmd"
- ], state="readonly", width=10)
- self.extension_dropdown.pack(side=tk.LEFT, padx=5)
- # Custom Extension Entry Field
- tk.Label(drive_frame, text="Enter Custom Extension:", fg="white", bg="#2e3f4f", font=("Times New Roman", 14)).pack(side=tk.LEFT, padx=5)
- self.custom_extension_entry = tk.Entry(drive_frame, textvariable=self.custom_extension, width=10, font=("Times New Roman", 12))
- self.custom_extension_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 Extensions", 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 File", 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 based on selected or custom extension."""
- self.file_listbox.delete(0, tk.END) # Clear previous results
- extension = self.custom_extension.get().strip() or self.selected_extension.get()
- # Ensure the extension starts with a dot
- if not extension.startswith('.'):
- extension = f".{extension}"
- selected_drive = self.selected_drive.get()
- drives_to_search = self.detected_drives[1:] if selected_drive == "Select All Drives" else [selected_drive]
- matching_files = []
- # Traverse drives and collect matching files
- for drive in drives_to_search:
- if os.path.exists(drive):
- for root, dirs, files in os.walk(drive):
- for file in files:
- if file.lower().endswith(extension.lower()):
- file_path = os.path.join(root, file)
- matching_files.append(file_path)
- self.file_listbox.insert(tk.END, file_path)
- self.update_idletasks() # Keep GUI responsive
- # Show a message if no files are found
- if not matching_files:
- messagebox.showinfo("Search Complete", f"No files found with extension '{extension}' in {selected_drive}.")
- def open_selected_file(self):
- """Open the selected file with the default program."""
- selected_file_index = self.file_listbox.curselection()
- if not selected_file_index:
- messagebox.showwarning("No File Selected", "Please select a file to open.")
- return
- selected_file = self.file_listbox.get(selected_file_index[0])
- try:
- os.startfile(selected_file) # Open file with default program
- except Exception as e:
- messagebox.showerror("Error Opening File", 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement