If you can't understand M2's hint, better you give up as the script you posted has so many mistakes that it must be fully recoded.
nx is a variable modifier NOT an internal variable and f is not declared in the For /F statement (you would code %%~nxf to catch file name and extension). More inside a For loop delayed variables must be referenced not usual ones (e.g. !filename! NOT %filename%).
The above is just the beginning , more will follow up!