Advertisement
Najeebsk

SEARCH-TEXT-INSIDE-FOLDER.pyw

Jul 23rd, 2024 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.83 KB | None | 0 0
  1. import os
  2. import subprocess
  3. import tkinter as tk
  4. from tkinter import filedialog, Text, Scrollbar, messagebox
  5. import re
  6.  
  7. # Path to VLC executable
  8. VLC_PATH = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
  9.  
  10. class FileSearcherApp:
  11.     def __init__(self, root):
  12.         self.root = root
  13.         self.root.title("Najeeb Inside Folder Text File Searcher")
  14.  
  15.         # Adjust the window size to fit the screen
  16.         screen_width = self.root.winfo_screenwidth()
  17.         screen_height = self.root.winfo_screenheight()
  18.         self.root.geometry(f"{screen_width}x{screen_height}")
  19.  
  20.         # Frame for buttons
  21.         self.frame = tk.Frame(self.root)
  22.         self.frame.pack(pady=10)
  23.  
  24.         # Entry to input search keyword
  25.         self.search_label = tk.Label(self.frame, text="Search Keyword:")
  26.         self.search_label.pack(side=tk.LEFT)
  27.  
  28.         self.search_entry = tk.Entry(self.frame)
  29.         self.search_entry.pack(side=tk.LEFT, padx=5)
  30.  
  31.         # Button to select folder
  32.         self.folder_btn = tk.Button(self.frame, text="Select Folder", command=self.select_folder)
  33.         self.folder_btn.pack(side=tk.LEFT, padx=5)
  34.  
  35.         # Button to start search
  36.         self.search_btn = tk.Button(self.frame, text="Search", command=self.search_files)
  37.         self.search_btn.pack(side=tk.LEFT, padx=5)
  38.  
  39.         # Button to save results
  40.         self.save_btn = tk.Button(self.frame, text="Save Results", command=self.save_results)
  41.         self.save_btn.pack(side=tk.LEFT, padx=5)
  42.  
  43.         # Toggle button to switch display mode
  44.         self.display_mode = tk.BooleanVar()
  45.         self.display_mode.set(True)  # Default to displaying file path and line number
  46.         self.toggle_btn = tk.Checkbutton(self.frame, text="Toggle Display Mode", variable=self.display_mode, command=self.toggle_display_mode)
  47.         self.toggle_btn.pack(side=tk.LEFT, padx=5)
  48.  
  49.         # Play button
  50.         self.play_button = tk.Button(self.frame, text="Play", command=self.play_channel)
  51.         self.play_button.pack(side=tk.LEFT, padx=5)
  52.  
  53.         # Text field for displaying results with a larger font size
  54.         self.result_text = Text(self.root, wrap='none', undo=True, width=80, height=20, font=("Times New Roman", 16))
  55.         self.result_text.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
  56.  
  57.         # Scrollbars for the text field
  58.         self.v_scroll = Scrollbar(self.result_text, orient=tk.VERTICAL, command=self.result_text.yview)
  59.         self.v_scroll.pack(side=tk.RIGHT, fill=tk.Y)
  60.         self.result_text['yscrollcommand'] = self.v_scroll.set
  61.  
  62.         self.h_scroll = Scrollbar(self.result_text, orient=tk.HORIZONTAL, command=self.result_text.xview)
  63.         self.h_scroll.pack(side=tk.BOTTOM, fill=tk.X)
  64.         self.result_text['xscrollcommand'] = self.h_scroll.set
  65.  
  66.         self.folder_path = ""
  67.  
  68.     def select_folder(self):
  69.         self.folder_path = filedialog.askdirectory()
  70.  
  71.     def search_files(self):
  72.         keyword = self.search_entry.get()
  73.         if not self.folder_path:
  74.             self.result_text.insert(tk.END, "Please select a folder first.\n")
  75.             return
  76.  
  77.         if not keyword:
  78.             self.result_text.insert(tk.END, "Please enter a keyword to search for.\n")
  79.             return
  80.  
  81.         self.result_text.delete(1.0, tk.END)  # Clear the text field
  82.  
  83.         for root, dirs, files in os.walk(self.folder_path):
  84.             for file in files:
  85.                 if file.endswith(('.txt', '.ls', '.ini', '.m3u', '.m3u8', '.py', '.pyw', '.ahk', '.bat', '.cmd', '.vbs', '.htm', '.html', '.au3', '.reg')):
  86.                     file_path = os.path.join(root, file)
  87.                     self.search_in_file(file_path, keyword)
  88.  
  89.     def search_in_file(self, file_path, keyword):
  90.         try:
  91.             with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
  92.                 lines = f.readlines()
  93.                 for i, line in enumerate(lines):
  94.                     if keyword.lower() in line.lower():
  95.                         if self.display_mode.get():
  96.                             self.result_text.insert(tk.END, f"{file_path} - Line {i+1}: {line}")
  97.                         else:
  98.                             self.result_text.insert(tk.END, f"{line}")
  99.         except Exception as e:
  100.             self.result_text.insert(tk.END, f"Error reading {file_path}: {str(e)}\n")
  101.  
  102.     def toggle_display_mode(self):
  103.         # Clear and re-search to update the display according to the new mode
  104.         self.search_files()
  105.  
  106.     def save_results(self):
  107.         results = self.result_text.get(1.0, tk.END)
  108.         if not results.strip():
  109.             messagebox.showwarning("No Results", "There are no results to save.")
  110.             return
  111.  
  112.         save_path = filedialog.asksaveasfilename(defaultextension=".txt",
  113.                                                  filetypes=[("Text files", "*.txt"), ("All files", "*.*")])
  114.         if save_path:
  115.             try:
  116.                 with open(save_path, 'w', encoding='utf-8') as file:
  117.                     file.write(results)
  118.                 messagebox.showinfo("Success", "Results saved successfully.")
  119.             except Exception as e:
  120.                 messagebox.showerror("Error", f"Failed to save results: {str(e)}")
  121.  
  122.     def play_channel(self):
  123.         try:
  124.             selected_text = self.result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  125.             url_match = re.search(r'(https?://\S+)', selected_text)
  126.             if url_match:
  127.                 url = url_match.group(0)
  128.                 subprocess.Popen([VLC_PATH, url])
  129.             else:
  130.                 messagebox.showwarning("Invalid Selection", "No valid URL found in the selected text.")
  131.         except tk.TclError:
  132.             messagebox.showwarning("No Selection", "Please select a line containing a valid URL to play.")
  133.  
  134. if __name__ == "__main__":
  135.     root = tk.Tk()
  136.     app = FileSearcherApp(root)
  137.     root.mainloop()
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement