def clean_filename(raw_name: str, to_lowercase: bool = False) -> str:
    """
    Cleans a filename of any invalid characters
    :param raw_name: input file basename to clean
    :param to_lowercase: cast string to lowercase
    :return: cleaned file basename
    """
    if not raw_name:
        raise ValueError
    invalid_chars = ('/', '\\', '*', '~', ':', '"', '<', '>', '|', '?')
    name = raw_name
    for char in invalid_chars:
        name = name.replace(char, "_")
    if to_lowercase:
        name = name.lower()
    return name