|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import datetime |
| 4 | + |
| 5 | +# --- Folder Tree Generator --- |
| 6 | +def generate_folder_map(root_folder, skip_folders): |
| 7 | + lines = [] |
| 8 | + |
| 9 | + def walk(folder, prefix=""): |
| 10 | + try: |
| 11 | + entries = sorted(os.listdir(folder)) |
| 12 | + except (PermissionError, FileNotFoundError): |
| 13 | + return |
| 14 | + |
| 15 | + entries = [e for e in entries if not e.startswith(".")] |
| 16 | + entries = [e for e in entries if not any(skip.lower() in e.lower() for skip in skip_folders)] |
| 17 | + |
| 18 | + for i, entry in enumerate(entries): |
| 19 | + path = os.path.join(folder, entry) |
| 20 | + connector = "└── " if i == len(entries) - 1 else "├── " |
| 21 | + if os.path.isdir(path): |
| 22 | + lines.append(f"{prefix}{connector}{entry}") |
| 23 | + extension = " " if i == len(entries) - 1 else "│ " |
| 24 | + walk(path, prefix + extension) |
| 25 | + else: |
| 26 | + lines.append(f"{prefix}{connector}{entry}") |
| 27 | + |
| 28 | + walk(root_folder) |
| 29 | + return "\n".join(lines) |
| 30 | + |
| 31 | +# --- Combine All Files Into One --- |
| 32 | +def dump_all_files_to_txt(root_folder, output_file, skip_extensions=None, skip_files=None, skip_folders=None): |
| 33 | + skip_extensions = skip_extensions or [] |
| 34 | + skip_files = skip_files or [] |
| 35 | + skip_folders = skip_folders or [] |
| 36 | + |
| 37 | + with open(output_file, "w", encoding="utf-8", errors="ignore") as outfile: |
| 38 | + # --- METADATA HEADER --- |
| 39 | + now = datetime.datetime.now().strftime("%B %d, %Y – %I:%M %p") |
| 40 | + outfile.write("===== PROJECT METADATA =====\n") |
| 41 | + outfile.write(f"Generated on: {now}\n") |
| 42 | + outfile.write(f"OS: {platform.system()} {platform.release()}\n") |
| 43 | + outfile.write(f"Root Folder: {os.path.abspath(root_folder)}\n\n") |
| 44 | + |
| 45 | + # --- ROUTE MAP (XML WRAPPED) --- |
| 46 | + outfile.write("===== ROUTE MAP / FOLDER STRUCTURE =====\n") |
| 47 | + outfile.write("<folder-structure>\n") |
| 48 | + folder_map = generate_folder_map(root_folder, skip_folders) |
| 49 | + outfile.write(folder_map if folder_map.strip() else "(No files found)") |
| 50 | + outfile.write("\n</folder-structure>\n") |
| 51 | + |
| 52 | + # --- BEGIN FILE CONTENTS --- |
| 53 | + outfile.write("\n\n===== BEGIN FILE CONTENTS =====\n") |
| 54 | + |
| 55 | + for foldername, subfolders, filenames in os.walk(root_folder): |
| 56 | + # Skip ignored folders |
| 57 | + if any(skip.lower() in foldername.lower() for skip in skip_folders): |
| 58 | + continue |
| 59 | + |
| 60 | + for filename in filenames: |
| 61 | + file_ext = os.path.splitext(filename)[1].lower() |
| 62 | + file_lower = filename.lower() |
| 63 | + |
| 64 | + if file_ext in skip_extensions or file_lower in skip_files: |
| 65 | + continue |
| 66 | + |
| 67 | + file_path = os.path.join(foldername, filename) |
| 68 | + try: |
| 69 | + with open(file_path, "r", encoding="utf-8", errors="ignore") as infile: |
| 70 | + outfile.write(f"\n\n===== FILE: {file_path} =====\n\n") |
| 71 | + outfile.write(infile.read()) |
| 72 | + except Exception as e: |
| 73 | + outfile.write(f"\n\n===== FILE: {file_path} (Could not read: {e}) =====\n\n") |
| 74 | + |
| 75 | + print(f"\n✅ Combined file created successfully: {output_file}") |
| 76 | + |
| 77 | +# --- MAIN EXECUTION --- |
| 78 | +if __name__ == "__main__": |
| 79 | + root_folder = r"C:\Users\Ashish jha\Desktop\PERSONAL\CodeToContext" # 👈 Change this |
| 80 | + output_file = "project_snapshot.txt" |
| 81 | + |
| 82 | + skip_extensions = [ |
| 83 | + ".jpg", ".jpeg", ".png", ".ico", ".bmp", ".svg", ".mp3", |
| 84 | + ".avi", ".mov", ".zip", ".rar", ".7z", ".tar", ".gz", |
| 85 | + ".exe", ".dll", ".bin", ".pdf" |
| 86 | + ] |
| 87 | + |
| 88 | + skip_files = [".ds_store", "package-lock.json", "yarn.lock", ".env", ".gitignore"] |
| 89 | + |
| 90 | + skip_folders = [ |
| 91 | + "node_modules", "dist", "build", "target", ".idea", ".vscode", |
| 92 | + "__pycache__", ".angular", ".next", ".git", ".svn", |
| 93 | + "venv", "env", "logs", "coverage", "out", "tmp" |
| 94 | + ] |
| 95 | + |
| 96 | + dump_all_files_to_txt(root_folder, output_file, skip_extensions, skip_files, skip_folders) |
0 commit comments