SHARE
    TWEET
    Najeebsk

    SEARCH-FILES.pyw

    Nov 27th, 2024
    281
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    Python 6.48 KB | None | 0 0
    1. import os
    2. import tkinter as tk
    3. from tkinter import ttk, messagebox
    4. from tkinter import filedialog
    5. class FileSearchApp(tk.Tk):
    6. def __init__(self):
    7. super().__init__()
    8. self.title("Najeeb Advanced File/Folder Searcher")
    9. self.geometry("900x600")
    10. self.configure(bg="#2e3f4f") # Background color
    11. # Variables
    12. self.search_query = tk.StringVar()
    13. self.selected_drive = tk.StringVar(value="Select All Drives")
    14. self.detected_drives = self.get_drives()
    15. # Create widgets
    16. self.create_widgets()
    17. def get_drives(self):
    18. """Detect all available drives on the system."""
    19. drives = []
    20. for drive in range(ord('A'), ord('Z') + 1):
    21. drive_letter = f"{chr(drive)}:"
    22. if os.path.exists(drive_letter):
    23. drives.append(drive_letter)
    24. return ["Select All Drives"] + drives
    25. def create_widgets(self):
    26. # Drive Selection Frame
    27. drive_frame = tk.Frame(self, bg="#2e3f4f")
    28. drive_frame.pack(pady=10)
    29. # Drive Dropdown
    30. tk.Label(drive_frame, text="Select Drive:", fg="white", bg="#2e3f4f", font=("Times New Roman", 14)).pack(side=tk.LEFT, padx=5)
    31. self.drive_dropdown = ttk.Combobox(drive_frame, textvariable=self.selected_drive, values=self.detected_drives, state="readonly", width=20)
    32. self.drive_dropdown.pack(side=tk.LEFT, padx=5)
    33. # Search Field
    34. #search_frame = tk.Frame(self, bg="#2e3f4f")
    35. #search_frame.pack(pady=10)
    36. tk.Label(drive_frame, text="Enter File/Folder Name:", fg="white", bg="#2e3f4f", font=("Times New Roman", 14)).pack(side=tk.LEFT, padx=5)
    37. self.search_entry = tk.Entry(drive_frame, textvariable=self.search_query, width=32, font=("Times New Roman", 14))
    38. self.search_entry.pack(side=tk.LEFT, padx=5)
    39. # Buttons (Search, Open, Save Results, Clear) in one line
    40. button_frame = tk.Frame(self, bg="#2e3f4f")
    41. button_frame.pack(pady=10)
    42. button_style = {"bg": "#007acc", "fg": "white", "font": ("Times New Roman", 14), "width": 17, "relief": tk.RAISED}
    43. self.search_button = tk.Button(button_frame, text="Search Files/Folders", command=self.search_files, **button_style)
    44. self.search_button.pack(side=tk.LEFT, padx=5)
    45. self.open_file_button = tk.Button(button_frame, text="Open Selected Item", command=self.open_selected_file, **button_style)
    46. self.open_file_button.pack(side=tk.LEFT, padx=5)
    47. self.save_button = tk.Button(button_frame, text="Save Results", command=self.save_results, **button_style)
    48. self.save_button.pack(side=tk.LEFT, padx=5)
    49. self.clear_button = tk.Button(button_frame, text="Clear Results", command=self.clear_results, **button_style)
    50. self.clear_button.pack(side=tk.LEFT, padx=5)
    51. # File Listbox
    52. self.result_frame = tk.Frame(self, bg="#2e3f4f")
    53. self.result_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
    54. self.file_listbox = tk.Listbox(self.result_frame, selectmode=tk.SINGLE, width=100, height=25, bg="#1c2833", fg="white", font=("Courier New", 10))
    55. self.file_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
    56. # Scrollbars
    57. scrollbar_vertical = tk.Scrollbar(self.result_frame, orient="vertical", command=self.file_listbox.yview)
    58. self.file_listbox.config(yscrollcommand=scrollbar_vertical.set)
    59. scrollbar_vertical.pack(side=tk.RIGHT, fill="y")
    60. def search_files(self):
    61. """Search files and folders based on the search query."""
    62. self.file_listbox.delete(0, tk.END) # Clear previous results
    63. query = self.search_query.get().strip().lower()
    64. if not query:
    65. messagebox.showwarning("Input Required", "Please enter a file or folder name to search.")
    66. return
    67. selected_drive = self.selected_drive.get()
    68. drives_to_search = self.detected_drives[1:] if selected_drive == "Select All Drives" else [selected_drive]
    69. matching_items = []
    70. # Traverse drives and collect matching files and folders
    71. for drive in drives_to_search:
    72. if os.path.exists(drive):
    73. for root, dirs, files in os.walk(drive):
    74. # Include hidden files and folders
    75. items = dirs + files
    76. for item in items:
    77. if query in item.lower():
    78. item_path = os.path.join(root, item)
    79. matching_items.append(item_path)
    80. self.file_listbox.insert(tk.END, item_path)
    81. self.update_idletasks() # Keep GUI responsive
    82. # Show a message if no items are found
    83. if not matching_items:
    84. messagebox.showinfo("Search Complete", f"No files or folders found containing '{query}' in {selected_drive}.")
    85. def open_selected_file(self):
    86. """Open the selected file or folder with the default program."""
    87. selected_item_index = self.file_listbox.curselection()
    88. if not selected_item_index:
    89. messagebox.showwarning("No Selection", "Please select a file or folder to open.")
    90. return
    91. selected_item = self.file_listbox.get(selected_item_index[0])
    92. try:
    93. os.startfile(selected_item) # Open with default program
    94. except Exception as e:
    95. messagebox.showerror("Error Opening Item", str(e))
    96. def save_results(self):
    97. """Save the search results to a file."""
    98. if self.file_listbox.size() == 0:
    99. messagebox.showwarning("No Results", "No results to save.")
    100. return
    101. file_path = filedialog.asksaveasfilename(
    102. defaultextension=".txt",
    103. filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
    104. title="Save Results As"
    105. )
    106. if not file_path:
    107. return
    108. try:
    109. with open(file_path, "w", encoding="utf-8") as file:
    110. for item in self.file_listbox.get(0, tk.END):
    111. file.write(item + "\n")
    112. messagebox.showinfo("Success", f"Results saved to {file_path}")
    113. except Exception as e:
    114. messagebox.showerror("Error Saving Results", str(e))
    115. def clear_results(self):
    116. """Clear the results from the Listbox."""
    117. self.file_listbox.delete(0, tk.END)
    118. if __name__ == "__main__":
    119. app = FileSearchApp()
    120. app.mainloop()
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /