import os
import re

# Set the directory containing your fs.N files
directory = 'block1'

# Compile a regex pattern to match the desired filename structure within the file contents
pattern = re.compile(rb'install/pkgs/([^ ]+\.inv)')

for filename in os.listdir(directory):
    # Construct the full path to the file
    filepath = os.path.join(directory, filename)
    
    # Open and read the file in binary mode
    with open(filepath, 'rb') as file:
        content = file.read()  # Read the entire file content
        # Search for the pattern in the file content
        match = pattern.search(content)
        
        if match:
            # Extract the new filename from the match
            new_filename = match.group(1).decode('latin1')  # Decode from bytes to string
            new_filepath = os.path.join(directory, new_filename)
            
            # Rename the file
            os.rename(filepath, new_filepath)
            print(f'Renamed "{filename}" to "{new_filename}"')
        else:
            print(f'Pattern not found in "{filename}". File not renamed.')

