HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to MS-DOS BatchVariablesConditions (IF)Loops (FOR)File OperationsParameters and Subroutines
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔀 Condition
🔁 Loop
⚙️ Commands & Flow
MS-DOS›Loops (FOR)

Loops (FOR)

Looping over lists, number ranges, and files with FOR.

Simple enough for a 5-year-old to understand

Loop over a list

In a FOR loop, the variable is written with a double percent sign instead of a single one (in a batch file).

Example code
1
for %%a in (apple pear cherry) do (
Loops for each item.
LoopLoops for each item.
2
echo %%a
Prints the item.
Output / InputPrints the item.
3
)
LoopOutput / Input
💡 Hint: A single percent sign is used in the command prompt, and a double percent sign is used in a .bat file.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.How is the FOR loop variable written in a batch file?

About this topic

In batch files, a loop is set up with a single command and performs different tasks depending on its options: traversing a list, generating a number range, walking through folders, or reading the lines of a file.

The loop variable is written with a percent sign, but twice in a script file and once on the command line. This difference stems from the percent sign carrying a special meaning when the script is parsed, and it is the most common reason why command line examples copied into a script fail to run.

Since the loop body is a block, the delayed expansion topic applies here as well. If you want to read a value you accumulated inside the loop during the same loop, you must use the exclamation mark notation.

What you will learn

  • Traversing over a list
  • Generating a number range
  • Reading file lines
  • The difference in variable syntax
  • Setting up a loop with a label

Who is this for?

  • Script writers processing a file list
  • Those moving command line examples into a script
  • Those trying to keep a counter

When to use?

If you are going to process files in a folder, the loop format that generates a file name list is the standard tool.

When it is necessary to read a text file line by line and split it into fields, the loop format with parsing options is used; the delimiter and field selection are specified in quotes.

Common mistakes

Wrong

@echo off
for %i in (a b c) do echo %i

In a script file, the loop variable is written with two percent signs; a single percent sign is for the command line. When written as a single one in the script, the command processor thinks it is an environment variable expansion and throws a syntax error. This is why examples copied from the command line do not work.

Correct

@echo off
for %%i in (a b c) do echo %%i

Wrong

@echo off
set liste=
for %%i in (a b c) do (
  set liste=%liste% %%i
)
echo %liste%

Since the percent-signed variable inside the block is expanded only once beforehand, the same old value is used on every turn; as a result, only the last element remains in the list. It is mandatory to enable delayed expansion and use the exclamation mark notation in loops that perform accumulation.

Correct

@echo off
setlocal enabledelayedexpansion
set liste=
for %%i in (a b c) do (
  set liste=!liste! %%i
)
echo !liste!
endlocal

Wrong

@echo off
for /l %%i in (1,5) do echo %%i

Three values are required in the number range format: start, step, and end. When two values are given, the loop behaves unexpectedly or does not run at all. It is important to remember that the middle value is the step, not the end; the order is different from other languages.

Correct

@echo off
for /l %%i in (1,1,5) do echo %%i

Frequently asked questions

How do I read file lines?

The loop format with parsing options is used; delimiter characters and which fields to retrieve are specified within quotes. Empty lines are skipped and lines starting with a semicolon are considered comments by default; there are options to change this behavior.

Can I exit a loop early?

There is no direct exit statement. The common solution is to use a flag variable and wrap the body in a condition, or to move the loop to a separate subroutine and return from there.

When is a labeled loop used?

For conditional repetitions whose count is not known beforehand. A label is defined, and at the end of the body, a conditional jump to that label is made. Since there is no delayed expansion issue, it is the smoothest way to keep a counter.

Summary

In a script, the loop variable is written with two percent signs.

The middle value in a number range is the step.

The loop body is a block.

Accumulation requires delayed expansion.

Labeled looping is suitable for conditional repetitions.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code