What Is a Variable?
A **variable** is basically a box that stores a value: text, numbers, paths, anything. You can change the value anytime and use it anywhere in your script.
set name=Kaloyan
echo Hello %name%
Output:
Hello Kaloyan
Important Rule
In Batch, you **set** variables without percent signs, and you **use** them with percent signs.
set age=12
echo You are %age% years old.
Asking the User for Input
You can ask the user to type something using set /p:
set /p username=Enter your username:
echo Welcome, %username%!
Variables Inside Loops (Delayed Expansion)
Batch normally doesn’t update variables inside loops unless you enable something called Delayed Expansion.
@echo off
setlocal enabledelayedexpansion
set count=0
for %%i in (1 2 3 4 5) do (
set /a count+=1
echo Count is now: !count!
)
Notice how variables inside loops use !exclamation marks! instead of %percent signs%.
Math With Variables (set /a)
You can do simple math:
set /a x=5+10
set /a y=x*2
echo %y%
Output:
30
Environment Variables
Windows has built-in system variables you can read:
echo Username: %USERNAME%
echo Windows Directory: %WINDIR%
echo Computer Name: %COMPUTERNAME%
Useful Example – Auto Backup Script
@echo off
set source=C:\Users\%USERNAME%\Documents
set backup=D:\Backups\%date%
echo Backing up files...
xcopy "%source%" "%backup%" /e /i /y
echo Backup complete!
pause
Variables make your scripts cleaner, reusable, and way easier to modify.
What’s Next?
Next you’ll learn how functions and procedures work in Python.