Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import subprocess
- import tkinter as tk
- from tkinter import filedialog, Text, Scrollbar, messagebox
- import re
- # Path to VLC executable
- VLC_PATH = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
- class FileSearcherApp:
- def __init__(self, root):
- self.root = root
- self.root.title("Najeeb Inside Folder Text File Searcher")
- # Adjust the window size to fit the screen
- screen_width = self.root.winfo_screenwidth()
- screen_height = self.root.winfo_screenheight()
- self.root.geometry(f"{screen_width}x{screen_height}")
- # Frame for buttons
- self.frame = tk.Frame(self.root)
- self.frame.pack(pady=10)
- # Entry to input search keyword
- self.search_label = tk.Label(self.frame, text="Search Keyword:")
- self.search_label.pack(side=tk.LEFT)
- self.search_entry = tk.Entry(self.frame)
- self.search_entry.pack(side=tk.LEFT, padx=5)
- # Button to select folder
- self.folder_btn = tk.Button(self.frame, text="Select Folder", command=self.select_folder)
- self.folder_btn.pack(side=tk.LEFT, padx=5)
- # Button to start search
- self.search_btn = tk.Button(self.frame, text="Search", command=self.search_files)
- self.search_btn.pack(side=tk.LEFT, padx=5)
- # Button to save results
- self.save_btn = tk.Button(self.frame, text="Save Results", command=self.save_results)
- self.save_btn.pack(side=tk.LEFT, padx=5)
- # Toggle button to switch display mode
- self.display_mode = tk.BooleanVar()
- self.display_mode.set(True) # Default to displaying file path and line number
- self.toggle_btn = tk.Checkbutton(self.frame, text="Toggle Display Mode", variable=self.display_mode, command=self.toggle_display_mode)
- self.toggle_btn.pack(side=tk.LEFT, padx=5)
- # Play button
- self.play_button = tk.Button(self.frame, text="Play", command=self.play_channel)
- self.play_button.pack(side=tk.LEFT, padx=5)
- # Text field for displaying results with a larger font size
- self.result_text = Text(self.root, wrap='none', undo=True, width=80, height=20, font=("Times New Roman", 16))
- self.result_text.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
- # Scrollbars for the text field
- self.v_scroll = Scrollbar(self.result_text, orient=tk.VERTICAL, command=self.result_text.yview)
- self.v_scroll.pack(side=tk.RIGHT, fill=tk.Y)
- self.result_text['yscrollcommand'] = self.v_scroll.set
- self.h_scroll = Scrollbar(self.result_text, orient=tk.HORIZONTAL, command=self.result_text.xview)
- self.h_scroll.pack(side=tk.BOTTOM, fill=tk.X)
- self.result_text['xscrollcommand'] = self.h_scroll.set
- self.folder_path = ""
- def select_folder(self):
- self.folder_path = filedialog.askdirectory()
- def search_files(self):
- keyword = self.search_entry.get()
- if not self.folder_path:
- self.result_text.insert(tk.END, "Please select a folder first.\n")
- return
- if not keyword:
- self.result_text.insert(tk.END, "Please enter a keyword to search for.\n")
- return
- self.result_text.delete(1.0, tk.END) # Clear the text field
- for root, dirs, files in os.walk(self.folder_path):
- for file in files:
- if file.endswith(('.txt', '.ls', '.ini', '.m3u', '.m3u8', '.py', '.pyw', '.ahk', '.bat', '.cmd', '.vbs', '.htm', '.html', '.au3', '.reg')):
- file_path = os.path.join(root, file)
- self.search_in_file(file_path, keyword)
- def search_in_file(self, file_path, keyword):
- try:
- with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
- lines = f.readlines()
- for i, line in enumerate(lines):
- if keyword.lower() in line.lower():
- if self.display_mode.get():
- self.result_text.insert(tk.END, f"{file_path} - Line {i+1}: {line}")
- else:
- self.result_text.insert(tk.END, f"{line}")
- except Exception as e:
- self.result_text.insert(tk.END, f"Error reading {file_path}: {str(e)}\n")
- def toggle_display_mode(self):
- # Clear and re-search to update the display according to the new mode
- self.search_files()
- def save_results(self):
- results = self.result_text.get(1.0, tk.END)
- if not results.strip():
- messagebox.showwarning("No Results", "There are no results to save.")
- return
- save_path = filedialog.asksaveasfilename(defaultextension=".txt",
- filetypes=[("Text files", "*.txt"), ("All files", "*.*")])
- if save_path:
- try:
- with open(save_path, 'w', encoding='utf-8') as file:
- file.write(results)
- messagebox.showinfo("Success", "Results saved successfully.")
- except Exception as e:
- messagebox.showerror("Error", f"Failed to save results: {str(e)}")
- def play_channel(self):
- try:
- selected_text = self.result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
- url_match = re.search(r'(https?://\S+)', selected_text)
- if url_match:
- url = url_match.group(0)
- subprocess.Popen([VLC_PATH, url])
- else:
- messagebox.showwarning("Invalid Selection", "No valid URL found in the selected text.")
- except tk.TclError:
- messagebox.showwarning("No Selection", "Please select a line containing a valid URL to play.")
- if __name__ == "__main__":
- root = tk.Tk()
- app = FileSearcherApp(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement