|It could be argued that SETLOCAL doesn't need to be used at all, unless you need delayed expansion. |
Anyways, your error message is because you keep calling SETLOCAL without a corresponding ENDLOCAL. This happens either because you're calling yourself recursively (typically because your script is named the same as some command), or because you never call ENDLOCAL at the end of your functions.
And SETLOCAL should only be used when you want to modify CMD's behavior for your script, or when you risk overwriting a variable whose value you want to save.
How To Ask Questions The Smart Way