import os

# Get directory of this script, and switch to it
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)

print("Current working directory:", os.getcwd())

extensions = ['B159', 'B160', 'B162']

all_files = os.listdir('.')
print("Files in current directory:")
for f in all_files:
    print(" -", f)

for ext in extensions:
    matching_files = sorted([
        f for f in all_files
        if f.lower().endswith(f'.{ext.lower()}')
    ])
    
    print(f"\nLooking for files ending with .{ext}...")
    if matching_files:
        print(f"Found {len(matching_files)} file(s):")
        for f in matching_files:
            print("  -", f)
        
        output_filename = f'COMBINED_ALL.{ext}'
        with open(output_filename, 'wb') as outfile:
            for fname in matching_files:
                with open(fname, 'rb') as infile:
                    outfile.write(infile.read())
        print(f"Created {output_filename}")
    else:
        print(f"No files found for extension .{ext}")
