Windows 10

For Loop Counting From 1 To N in a Batch File

In this tutorial, we are going to see how to increment counter using For Loop. The batch file contains a series of DOS (Disk Operating System) instructions. It allows triggering the execution of commands found in this file.
 

Example 1: Count From 1 To N in a Batch File Using For Loop

The following example use for loop to count From 1 To 10:

@echo off

for /L  %%a in (1,1,10) Do echo %%a

Output:

1
2
3
4
5
6
7
8
9
10

 

 

Example 2: Count From 1 To N in a Batch File Using Goto Statement

The following example use goto statement to count From 1 To 10:

@echo off

set c=1

:begin
echo %c%
set /a c=%c%+1
if %c% EQU 10 goto end
goto begin
:end

Output:

1
2
3
4
5
6
7
8
9

 

Leave a Reply

Your email address will not be published. Required fields are marked *