def generate_function_to_remove_element_from_list(functionName, listName):
    """Generate a Batch script function to remove element from a list.

    As in Batch scripts it is not possible to dereference twice a variable,
    it is easier to generate a function for each variable from which we want
    to remove an element.

    Keyword arguments:
    functionName -- the function name
    listName -- the name of the environment variable that contains the list
    """
    return f"""
@REM code below borrowed from https://stackoverflow.com/a/1430570 (CC-BY-SA 2.5)
@REM Author: Cheeso, Sep 16, 2009

:{functionName}
SETLOCAL ENABLEDELAYEDEXPANSION

@REM  ~fs = remove quotes, full path, short names
set fqElement=%~fs1

@REM convert path to a list of quote-delimited strings, separated by spaces
set fpath="%{listName}:;=" "%"

@REM iterate through those path elements
for %%p in (%fpath%) do (
    @REM  ~fs = remove quotes, full path, short names
    set p2=%%~fsp
    @REM is this element NOT the one we want to remove?
    if /i NOT "!p2!"=="%fqElement%" (
        if _!tpath!==_ (set tpath=%%~p) else (set tpath=!tpath!;%%~p)
    )
)

set {listName}=!tpath!
ENDLOCAL & set {listName}=%tpath%
goto :EOF
"""