Once you can write simple linear scripts, it's time to add logic. Batch files support conditional execution (if), loops (for), and jumping to specific sections (goto).
Check if a file exists!
Flow Control Concepts
1. Conditional Execution (IF)
Execute commands only when certain conditions are met.
if exist config.txt (
echo Config found!
) else (
echo Config missing!
)
2. Loops (FOR)
Repeat a command for a set of files or numbers.
:: Print numbers 1 to 5
for /L %%i in (1,1,5) do echo %%i
3. Goto and Labels
Jump to different parts of your script.
:Start
echo Working...
goto End
:End
echo Done.
Real-World Examples
1. Creating a backup if a file exists
Safeguard your data automatically.
Choose an option and the terminal output updates immediately.
2. Looping through files
Process every .txt file in the current folder.
Choose an option and the terminal output updates immediately.
(Note: Use %f in command line, but %%f in batch files)
Knowledge Check
1 / 3Which keyword is used for conditional logic?
References
These documentation links provide authoritative details for the commands used in this article.