|Hi lee123abc, I got your message and here I am.|
A post is not well suited to host a lesson on scripting style that probably looks cumbersome to you due to your poor knowledge of NT batch principles (see the ^ issue). So I just state some practical rules avoiding general theory.
- There are two basic SETLOCAL statements
setlocal (with no operands)
- Both must be coded at the beginning of the script just after the @echo off initial command to suppress screen echo.
- The setlocal shields the system environment from the manipulations performed by the batch as setting or deleting variables.
- Actually you need to issue the long format of setlocal to enable the use of dynamic variables marked by ! inside For loops and if ... (commands) else (commands) statements.
- When the batch ends an ENDLOBAL is automatically issued, so you never need to code that command.
As I said these are raw rules as a detailed description of setlocal/endlocal requires a clear understanding of the mechanism related to environment inheritance and other issues about the behavior of cmd.exe in a multitasking system that are beyond a quick note.
To be short and avoid any confusion: issue setlocal at the beginning of your code (if needed to enable delayed expansion) and don't bother with endlocal, i.e. forget it.