You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.1 KiB
Python

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 get_language_abbreviation(file_name):
# Extract the language abbreviation from the file name
parts = file_name.split('(')
if len(parts) > 1:
language_abbreviation = parts[-1].split(')')[0]
return language_abbreviation.strip()
return ''
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)
# Extract the language abbreviation from the file name
language = get_language_abbreviation(file)
# Generate the new file name
file_name, file_extension = os.path.splitext(file)
new_file_name = f"{file_name} ({language}){file_extension}"
target_file = os.path.join(target_dir, new_file_name)
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)
root_dir = pathlib.Path(__file__).parent.absolute()
source_directory = config.get('Settings', 'source_root_dir')
source_dir_full = root_dir / source_directory
if not os.path.exists(source_dir_full):
pathlib.Path.mkdir(source_dir_full, exist_ok= True)
target_directory = config.get('Settings', 'target_root_dir')
target_dir_full = root_dir / target_directory
if not os.path.exists(target_dir_full):
pathlib.Path.mkdir(target_dir_full, exist_ok= True)
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}')