| name | batch_file_organization |
| description | Discovers PDF, DOCX, and PPTX files in a directory (handling hidden characters and extensions robustly), extracts their text, determines their subject, and moves them into organized folders. |
import os
import shutil
from glob import glob
def organize_files(source_dir):
"""
Orchestrates the discovery, extraction, classification, and moving of files.
"""
subjects = ["LLM", "trapped_ion_and_qc", "black_hole", "DNA", "music_history"]
for subject in subjects:
if not os.path.exists(os.path.join(source_dir, subject)):
os.makedirs(os.path.join(source_dir, subject))
patterns = ['*.pdf', '*.docx', '*.pptx', '*.PDF', '*.DOCX', '*.PPTX']
files_to_process = []
for pattern in patterns:
files_to_process.extend(glob(os.path.join(source_dir, pattern)))
for file_path in files_to_process:
filename = os.path.basename(file_path)
if any(subject in file_path for subject in subjects):
continue
text = ""
if filename.lower().endswith('.pdf'):
text = extract_pdf_text(file_path)
elif filename.lower().endswith(('.docx', '.pptx')):
text = extract_office_text(file_path)
category = classify_document(text)
target_path = os.path.join(source_dir, category, filename)
try:
shutil.move(file_path, target_path)
except Exception as e:
print(f"Error moving {filename}: {e}")