It seems that since I use SET to declare my variables in batch script, if I run it multiple times in cmd, the variable value will persist unless I explicitly reset them.
Do I have to use setlocal and endlocal to make sure the variables from one run doesn't persist over to another, without shutting down the CMD?
However, you can clear the value of an environment variable using Command Prompt. To unset an environment variable from Command Prompt, type the command setx variable_name “”.
If you want to run a command and then clear the screen, put an ampersand at the end of your command, then the cls command. In the example above, if the current directory contained a batch file called test. bat, it would run the batch file, and then clear screen.
When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.
%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used.
Yes, you should use SETLOCAL. That will localize any changes such that the old environment will be restored once ENDLOCAL is issued.
When all script processing finishes and you are returned to a command line context, there is an implicit ENDLOCAL issued for every active SETLOCAL. There is no need to explicitly issue ENDLOCAL.
Also, if your script (or routine) is CALLed, then when the CALL completes there is an implicit ENDLOCAL for every active SETLOCAL that was issued within the CALLed routine. No need to put ENDLOCAL at end of a routine, (though it doesn't hurt)
For example
@echo off
set var=pre-CALL value
echo var=%var%
call :test
echo var=%var%
exit /b
:test
setlocal
set var=within CALL value
echo var=%var%
exit /b
output:
var=pre-CALL value
var=within CALL value
var=pre-CALL value
ENDLOCAL within a CALLed routine will never rollback a SETLOCAL that was issued before the CALL. For example.
@echo off
setlocal
set var=VALUE 1
setlocal
set var=VALUE 2
echo before call: var=%var%
call :test
echo after call: var=%var%
endlocal
echo after endlocal: var=%var%
exit /b
:test
setlocal
set var=VALUE 3
echo within local CALL context: var=%var%
endlocal
echo within CALL after 1st endlocal: var=%var%
endlocal
echo within CALL cannot endlocal to before CALL state: var=%var%
exit /b
Result:
before call: var=VALUE 2
within local CALL context: var=VALUE 3
within CALL after 1st endlocal: var=VALUE 2
within CALL cannot endlocal to before CALL state: var=VALUE 2
after call: var=VALUE 2
after endlocal: var=VALUE 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With