2

My code looks like this so far:

:start
set a=0
echo Enter Number:
set /p b%a%=
set /a a=%a%+1
if %a% lss 5 goto start
set x=1
:show
echo Number 1: %b%a%%
if %x% lss 5 goto show  

Is this possible? It's like an array but technically it's not.

  • Maybe with some ugly hack: You could generate batch files at runtime with echo which then would contain the actual set /p statements. – Florian Straub Jun 23 '16 at 16:05
  • Not sure what you are trying to achieve, I guess you might be interested in this post: [Batch: Parse TXT Lines into Array](http://stackoverflow.com/a/30404094)... – aschipfl Jun 23 '16 at 16:44

1 Answers1

2

Yes, this is possible as the batch code below demonstrates.

@echo off
setlocal EnableDelayedExpansion
set "a=1"
:LoopEnterNumbers
set /P "b%a%=Enter Number: "
set /A a+=1
if %a% lss 6 goto LoopEnterNumbers
set "a=1"
:LoopDisplayNumbers
echo Number %a%: !b%a%!
set /A a+=1
if %a% lss 6 goto LoopDisplayNumbers
endlocal

start is a Windows standard command. It is possible but not advisable to use it as label.

The prompt text can be specified on command set /P.
See How to set environment variables with spaces? for more details.

Everything after set /A is interpreted as arithmetic expression and += is supported, too.

Usage of delayed environment variable expansion is needed to reference value of an environment variable with another environment variable in name.

BTW: The command set b outputs all environment variables in format name=value starting with the character b. With a more unique name this is often a better method to output variables than the used loop above.

@echo off
setlocal EnableDelayedExpansion
set a=1
:LoopEnterNumbers
set /P "MyNumber#%a%=Enter Number: "
set /A a+=1
if %a% lss 6 goto LoopEnterNumbers
for /F "tokens=2,3 delims=#=" %%I in ('set MyNumber#') do echo Number %%I%: %%J
endlocal

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • set /?
  • setlocal /?
Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143