import os
def search_keywords_in_files(keywords, directory):
# Membuat dictionary untuk menyimpan hasil berdasarkan keyword
results = {keyword: [] for keyword in keywords}
# Mendapatkan semua file .txt dalam direktori
txt_files = [f for f in os.listdir(directory) if f.endswith('.txt')]
for filename in txt_files:
file_path = os.path.join(directory, filename)
try:
with open(file_path, 'r', encoding='utf-8', errors='replace') as file:
for line in file:
for keyword in keywords:
if keyword in line:
results[keyword].append(line.strip())
except Exception as e:
print(f"❌ ERROR saat membaca file '{filename}': {e}")
# Membuat direktori OUTPUT jika belum ada
output_directory = os.path.join(directory, 'OUTPUT')
os.makedirs(output_directory, exist_ok=True)
# Menyimpan hasil ke file terpisah di direktori OUTPUT
for keyword, lines in results.items():
if lines: # Hanya simpan jika ada hasil
# Ganti karakter yang tidak valid dengan '_'
safe_keyword = keyword.replace('/', '_').replace(':', '_').replace('.', '_')
output_filename = f"{safe_keyword}.txt"
output_path = os.path.join(output_directory, output_filename)
with open(output_path, 'w', encoding='utf-8') as output_file:
output_file.write("\n".join(lines))
print(f"✅ Hasil untuk '{keyword}' disimpan di '{output_path}'")
if __name__ == "__main__":
# Daftar keyword yang ingin dicari
keywords = [
"phpmyadmin/", "adminer.php"
]
# Input direktori
directory = input("📁 Nama direktori: ").strip()
if os.path.isdir(directory):
search_keywords_in_files(keywords, directory)
else:
print(f"❌ Direktori '{directory}' tidak ditemukan.")