mayankjoin3

python search string and export in txt file

Jun 26th, 2026
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. import os
  2. from datetime import datetime
  3.  
  4. # ---------- CONFIG ----------
  5. ROOT_DIR = r"C:\Users\Mayank\Documents\GitHub\ERP"
  6.  
  7. SEARCH_STRINGS = [
  8.     "ini_set('display_errors', 1);",
  9.     'ini_set("display_errors", 1);',
  10.     "ini_set('display_startup_errors', 1);",
  11.     'ini_set("display_startup_errors", 1);',
  12.     "error_reporting(E_ALL);"
  13. ]
  14.  
  15. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  16. OUTPUT_FILE = f"php_error_reporting_files_{timestamp}.txt"
  17.  
  18. matches = []
  19.  
  20. # ---------- SCAN ----------
  21. for root, _, files in os.walk(ROOT_DIR):
  22.     for file in files:
  23.  
  24.         if not file.lower().endswith(".php"):
  25.             continue
  26.  
  27.         full_path = os.path.abspath(os.path.join(root, file))
  28.  
  29.         try:
  30.             with open(full_path, "r", encoding="utf-8", errors="ignore") as f:
  31.                 content = f.read()
  32.  
  33.             # Match if ANY of the three statements are present
  34.             if any(s in content for s in SEARCH_STRINGS):
  35.  
  36.                 matches.append(full_path)
  37.  
  38.         except Exception as e:
  39.             print(f"Error reading {full_path}: {e}")
  40.  
  41. # ---------- EXPORT ----------
  42. with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
  43.  
  44.     f.write(f"Total files found: {len(matches)}\n\n")
  45.  
  46.     for path in sorted(matches):
  47.  
  48.         f.write(path + "\n")
  49.  
  50.     f.write("\n\n================ DELETE COMMANDS ================\n\n")
  51.  
  52.     for path in sorted(matches):
  53.  
  54.         f.write(f'del "{path}"\n')
  55.  
  56. print(f"\nFound {len(matches)} PHP files.")
  57. print(f"Output written to: {OUTPUT_FILE}")
Advertisement
Add Comment
Please, Sign In to add comment