import os import sys # --- Configuration --- OUTPUT_FILENAME = "!combo.txt" # ALLOWED_EXTENSIONS = (".py", ".md", ".env", ".bat") # Removed this line # --- End Configuration --- def combine_files_in_current_folder(): """ Combines *all files* in the script's current directory (and its subdirectories) into a single output file, sorted alphabetically by filename, with separator tags and overall ``` wrapping. Excludes the script itself and the output file. """ try: # Determine the script's own path and name if getattr(sys, 'frozen', False): # If running as a bundled executable (e.g., PyInstaller) script_full_path = os.path.abspath(sys.executable) else: # If running as a .py script script_full_path = os.path.abspath(__file__) script_dir = os.path.dirname(script_full_path) script_basename = os.path.basename(script_full_path) output_filepath = os.path.join(script_dir, OUTPUT_FILENAME) print(f"Running in: {script_dir}") print(f"Excluding self: {script_basename}") print(f"Output will be: {output_filepath}") print("-" * 30) file_paths_to_process = [] # Walk through the directory tree starting from the script's directory for root, _, files in os.walk(script_dir): for filename in files: # Construct full path to compare with script_full_path current_file_full_path = os.path.join(root, filename) # 1. Exclude the script itself # 2. Exclude the output file if it already exists and is being processed if (os.path.normpath(current_file_full_path) == os.path.normpath(script_full_path) or filename == OUTPUT_FILENAME): continue # Removed the ALLOWED_EXTENSIONS check. All files (not excluded above) will be added. file_paths_to_process.append((filename, current_file_full_path)) # Sort files alphabetically by their base filename file_paths_to_process.sort(key=lambda x: x[0]) if not file_paths_to_process: print(f"No files found (other than this script or the output file).") # Create an empty combo file with open(output_filepath, 'w', encoding='utf-8') as outfile: outfile.write("```\n```\n") print(f"Empty '{output_filepath}' created.") return print(f"\nFound {len(file_paths_to_process)} files to combine:") for basename, _ in file_paths_to_process: print(f" - {basename}") print("-" * 30) with open(output_filepath, 'w', encoding='utf-8') as outfile: outfile.write("```\n") # Start with ``` for basename, full_path in file_paths_to_process: relative_path = os.path.relpath(full_path, script_dir) print(f"Processing: {basename} (from {relative_path})") try: # Attempt to read as text. For binary files, this might produce # garbled output or raise an error if not for errors='replace'. # errors='replace' will insert a replacement character for undecodable bytes. with open(full_path, 'r', encoding='utf-8', errors='replace') as infile: content = infile.read() # Use relative path in the tag for clarity if files have same name in different subdirs outfile.write(f"<{relative_path}>\n") outfile.write(content) outfile.write(f"\n\n\n") # Add extra newline for readability except Exception as e: print(f" Error reading file {full_path}: {e}") outfile.write(f"<{relative_path}>\n") outfile.write(f"Error reading file: {e}\nContent might be incomplete or missing.\n") outfile.write(f"\n\n") outfile.write("```\n") # End with ``` print(f"\nSuccessfully combined files into '{output_filepath}'") except Exception as e: print(f"An unexpected error occurred: {e}") import traceback traceback.print_exc() if __name__ == "__main__": combine_files_in_current_folder() # Keep the console window open until the user presses Enter # This is helpful when double-clicking the script on Windows. try: input("\nPress Enter to exit...") except EOFError: # Happens if stdin is not available (e.g. some CI environments) pass