import os import pathlib import shutil import configparser def load_path_config(): conf = configparser.ConfigParser() root_dir = pathlib.Path(__file__).parent.absolute() conf_dir = root_dir / "config.ini" conf.read(conf_dir) paths = conf['Paths'] return paths def replicate_directory_structure(source_path:str, target_path:str, file_extensions, max_depth=None): for root, dirs, files in os.walk(source_path): # Get the relative path of the current directory relative_path = os.path.relpath(root, source_path) target_dir = os.path.join(target_path, relative_path) # Calculate the depth of the current directory current_depth = relative_path.count(os.sep) # Create the corresponding directory in the target path corr_path = target_dir.split('[')[0] os.makedirs(corr_path, exist_ok=True) # Copy files with the specified extensions to the target directory for file in files: if any(file.endswith(ext) for ext in file_extensions): source_file = os.path.join(root, file) target_file = os.path.join(corr_path, file) shutil.copy2(source_file, target_file) # Check if the maximum depth has been reached if max_depth is not None and current_depth >= max_depth: del dirs[:] # Do not traverse deeper into subdirectories if __name__ == "__main__": config_file = 'config.ini' # Read settings from the config file config = configparser.ConfigParser() config.read(config_file) source_directory = config.get('Settings', 'source_root_dir') target_directory = config.get('Settings', 'target_root_dir') max_depth = config.getint('Settings', 'max_depth') # Read file extensions from the config file file_extensions = [ext.strip() for ext in config.get('FileExtensions', 'extensions').split(',')] print('Scan Roms...') replicate_directory_structure(source_directory, target_directory, file_extensions, max_depth) print(f'Scan Completed. Prepped Folder Structure saved at {target_directory}')