In batch files variables should be referred as %%variable
On command lines variables should be referred as %variable
For example,
## On the command line
FOR /L %a in (1,1,254) DO @echo 172.31.5.%a
## In a batch file
FOR /L %%a in (1,1,254) DO @echo 172.31.5.%%a
|