Advertisement
Najeebsk

SEARCH-EXT.pyw

Nov 27th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.04 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import ttk, messagebox
  4. from tkinter import filedialog
  5.  
  6. class FileSearchApp(tk.Tk):
  7.     def __init__(self):
  8.         super().__init__()
  9.         self.title("Najeeb Advanced File Extensions Search")
  10.         self.geometry("900x600")
  11.         self.configure(bg="#2e3f4f")  # Background color
  12.  
  13.         # Variables
  14.         self.selected_extension = tk.StringVar(value=".exe")
  15.         self.custom_extension = tk.StringVar()
  16.         self.selected_drive = tk.StringVar(value="Select All Drives")
  17.         self.detected_drives = self.get_drives()
  18.  
  19.         # Create widgets
  20.         self.create_widgets()
  21.  
  22.     def get_drives(self):
  23.         """Detect all available drives on the system."""
  24.         drives = []
  25.         for drive in range(ord('A'), ord('Z') + 1):
  26.             drive_letter = f"{chr(drive)}:"
  27.             if os.path.exists(drive_letter):
  28.                 drives.append(drive_letter)
  29.         return ["Select All Drives"] + drives
  30.  
  31.     def create_widgets(self):
  32.         # Drive Selection Frame
  33.         drive_frame = tk.Frame(self, bg="#2e3f4f")
  34.         drive_frame.pack(pady=10)
  35.  
  36.         # Drive Dropdown
  37.         tk.Label(drive_frame, text="Select Drive:", fg="white", bg="#2e3f4f", font=("Times New Roman", 14)).pack(side=tk.LEFT, padx=5)
  38.         self.drive_dropdown = ttk.Combobox(drive_frame, textvariable=self.selected_drive, values=self.detected_drives, state="readonly", width=20)
  39.         self.drive_dropdown.pack(side=tk.LEFT, padx=5)
  40.  
  41.         # Extension Selection Frame
  42.         #extension_frame = tk.Frame(self, bg="#2e3f4f")
  43.         #extension_frame.pack(pady=10)
  44.  
  45.         # Dropdown Menu for Extensions
  46.         tk.Label(drive_frame, text="Select Extension:", fg="white", bg="#2e3f4f", font=("Times New Roman", 14)).pack(side=tk.LEFT, padx=5)
  47.         self.extension_dropdown = ttk.Combobox(drive_frame, textvariable=self.selected_extension, values=[
  48.             ".exe", ".txt", ".bat", ".ini", ".reg", ".ahk", ".au3", ".m3u", ".m3u8", ".py", ".pyw", ".vbs", ".cmd"
  49.         ], state="readonly", width=10)
  50.         self.extension_dropdown.pack(side=tk.LEFT, padx=5)
  51.  
  52.         # Custom Extension Entry Field
  53.         tk.Label(drive_frame, text="Enter Custom Extension:", fg="white", bg="#2e3f4f", font=("Times New Roman", 14)).pack(side=tk.LEFT, padx=5)
  54.         self.custom_extension_entry = tk.Entry(drive_frame, textvariable=self.custom_extension, width=10, font=("Times New Roman", 12))
  55.         self.custom_extension_entry.pack(side=tk.LEFT, padx=5)
  56.  
  57.         # Buttons (Search, Open, Save Results, Clear) in one line
  58.         button_frame = tk.Frame(self, bg="#2e3f4f")
  59.         button_frame.pack(pady=10)
  60.  
  61.         button_style = {"bg": "#007acc", "fg": "white", "font": ("Times New Roman", 14), "width": 17, "relief": tk.RAISED}
  62.  
  63.         self.search_button = tk.Button(button_frame, text="Search Extensions", command=self.search_files, **button_style)
  64.         self.search_button.pack(side=tk.LEFT, padx=5)
  65.  
  66.         self.open_file_button = tk.Button(button_frame, text="Open Selected File", command=self.open_selected_file, **button_style)
  67.         self.open_file_button.pack(side=tk.LEFT, padx=5)
  68.  
  69.         self.save_button = tk.Button(button_frame, text="Save Results", command=self.save_results, **button_style)
  70.         self.save_button.pack(side=tk.LEFT, padx=5)
  71.  
  72.         self.clear_button = tk.Button(button_frame, text="Clear Results", command=self.clear_results, **button_style)
  73.         self.clear_button.pack(side=tk.LEFT, padx=5)
  74.  
  75.         # File Listbox
  76.         self.result_frame = tk.Frame(self, bg="#2e3f4f")
  77.         self.result_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
  78.  
  79.         self.file_listbox = tk.Listbox(self.result_frame, selectmode=tk.SINGLE, width=100, height=25, bg="#1c2833", fg="white", font=("Courier New", 10))
  80.         self.file_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  81.  
  82.         # Scrollbars
  83.         scrollbar_vertical = tk.Scrollbar(self.result_frame, orient="vertical", command=self.file_listbox.yview)
  84.         self.file_listbox.config(yscrollcommand=scrollbar_vertical.set)
  85.         scrollbar_vertical.pack(side=tk.RIGHT, fill="y")
  86.  
  87.     def search_files(self):
  88.         """Search files based on selected or custom extension."""
  89.         self.file_listbox.delete(0, tk.END)  # Clear previous results
  90.         extension = self.custom_extension.get().strip() or self.selected_extension.get()
  91.        
  92.         # Ensure the extension starts with a dot
  93.         if not extension.startswith('.'):
  94.             extension = f".{extension}"
  95.  
  96.         selected_drive = self.selected_drive.get()
  97.         drives_to_search = self.detected_drives[1:] if selected_drive == "Select All Drives" else [selected_drive]
  98.  
  99.         matching_files = []
  100.  
  101.         # Traverse drives and collect matching files
  102.         for drive in drives_to_search:
  103.             if os.path.exists(drive):
  104.                 for root, dirs, files in os.walk(drive):
  105.                     for file in files:
  106.                         if file.lower().endswith(extension.lower()):
  107.                             file_path = os.path.join(root, file)
  108.                             matching_files.append(file_path)
  109.                             self.file_listbox.insert(tk.END, file_path)
  110.                             self.update_idletasks()  # Keep GUI responsive
  111.  
  112.         # Show a message if no files are found
  113.         if not matching_files:
  114.             messagebox.showinfo("Search Complete", f"No files found with extension '{extension}' in {selected_drive}.")
  115.  
  116.     def open_selected_file(self):
  117.         """Open the selected file with the default program."""
  118.         selected_file_index = self.file_listbox.curselection()
  119.         if not selected_file_index:
  120.             messagebox.showwarning("No File Selected", "Please select a file to open.")
  121.             return
  122.  
  123.         selected_file = self.file_listbox.get(selected_file_index[0])
  124.         try:
  125.             os.startfile(selected_file)  # Open file with default program
  126.         except Exception as e:
  127.             messagebox.showerror("Error Opening File", str(e))
  128.  
  129.     def save_results(self):
  130.         """Save the search results to a file."""
  131.         if self.file_listbox.size() == 0:
  132.             messagebox.showwarning("No Results", "No results to save.")
  133.             return
  134.  
  135.         file_path = filedialog.asksaveasfilename(
  136.             defaultextension=".txt",
  137.             filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
  138.             title="Save Results As"
  139.         )
  140.         if not file_path:
  141.             return
  142.  
  143.         try:
  144.             with open(file_path, "w", encoding="utf-8") as file:
  145.                 for item in self.file_listbox.get(0, tk.END):
  146.                     file.write(item + "\n")
  147.             messagebox.showinfo("Success", f"Results saved to {file_path}")
  148.         except Exception as e:
  149.             messagebox.showerror("Error Saving Results", str(e))
  150.  
  151.     def clear_results(self):
  152.         """Clear the results from the Listbox."""
  153.         self.file_listbox.delete(0, tk.END)
  154.  
  155. if __name__ == "__main__":
  156.     app = FileSearchApp()
  157.     app.mainloop()
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement