Tutorial by Examples

To create a simple variable and assign it to a value or string use the SET command: SET var=10 Here, the code declares a new variable var with a value of 10. By default all variables are stored internally as strings; this means that the value 10 is no different to foo1234 or Hello, World! Notes...
echo %var% This code will echo the value of var If setLocal EnableDelayedExpansion is used, the following will echo the value of var (the standard expression %var% will not work in that context). echo !var! In batch files, variables can be used in any context, including as parts of commands ...
Unlike other programming languages, in a batch file a variable is substituted by its actual value before the batch script is run. In other words, the substitution is made when the script is read into memory by the command processor, not when the script is later run. This enables the use of variable...
When multiple variables are defined at the beginning of the batch, a short definition form may be used by employing a replacement string. @echo off set "vars=_A=good,_B=,_E=bad,_F=,_G=ugly,_C=,_H=,_I=,_J=,_K=,_L=,_D=6 set "%vars:,=" & set "%" for /f %%l in ('set _'...
It is possible to create a set of variables that can act similar to an array (although they are not an actual array object) by using spaces in the SET statement: @echo off SET var=A "foo bar" 123 for %%a in (%var%) do ( echo %%a ) echo Get the variable directly: %var% Result: ...
set var=10 set /a var=%var%+10 echo %var% The final value of var is 20. The second line is not working within a command block used for example on an IF condition or on a FOR loop as delayed expansion would be needed instead of standard environment variable expansion. Here is another, better w...
Using the /p switch with the SET command you can define variables from an Input. This input can be a user Input (keyboard) : echo Enter your name : set /p name= echo Your name is %name% Which can be simplified like this : set /p name=Enter your name : echo Your name is %name% Or you can...

Page 1 of 1