Thursday, November 6, 2025

The SET statement in Batch Script and Loop Variable

Some Facts:

  • The set statement is used to declare and initialize a variable.
  • The variable initialization inside quote is considered best practice.
  • The replaceable parameter is prefixed with % on command line window or %% in batch file. 
  • Note that % is not both side of replaceable parameter as we do with variables declared with set statement.
  • The replaceable parameters are part of loop implicit varibles.
  • The SET statement is used to declare and initialize variable explicitly.
  • The loop variable is implicitly declared.
  • The SET variable can be of more than one letter but loop variable must be of single letter.

@echo off

set x=1222
set "y=234"
set "z=ajeet"
set "myVariable=Ram Krishna"

echo %x% %y% %z% %myVariable%

REM FOR /L implies for loop for a set of numbers
REM (1,3,44) implies (start,step,end) i.e. number between 1 and 44 and step value is 3
FOR /l %%n in (1,3,44) DO (echo %%n)

pause

No comments:

Post a Comment

Hot Topics