# /Volumes/ka1tbr2/ht/mking/anyt.py
import os
import re

# Define the source directory
source_dir = "../../i"

# Define the destination directory
destination_dir = "/Volumes/ka1tbr2/ht/mking/anyt"

# Function to create directories if they don't exist
def create_directories():
    if not os.path.exists(destination_dir):
#       os.makedirs(destination_dir, mode=0755)
        os.makedirs(destination_dir, mode=755)
    
    return

# Check if source directory exists
if not os.path.exists(source_dir):
    print(f"Error: Source directory '{source_dir}' does not exist.")
    exit()

# Get list of files in the source directory
try:
    files = os.listdir(source_dir)
except OSError:
    print("Error: Could not list files in the source directory.")
    exit()

# Filter and copy files starting with "anyt_"
file_list = []
for file in files:
    # Check if filename starts with 'anyt_' using fnmatch
    if re.match(r"anyt\._", file):
        full_path = os.path.join(source_dir, file)
        dest_full_path = os.path.join(destination_dir, file)
        try:
            # Copy the file
            shutil.copy(full_path, destination_dir)
            file_list.append(file)
        except Exception as e:
            print(f"Error: Failed to copy '{file}'.")
            exit()

# Create the output text file
output_file = os.path.join(destination_dir, "anyt.txt")

with open(output_file, 'w') as f:
    for file in file_list:
        f.write(f"{file}\n")

print("All files starting with 'anyt_' have been copied and listed.")

