0

All I am trying is to store a string in an array and then access them using variable as index. I have tried Delayed Expansion, but I don't know what is wrong with code. Please help. The test.cmd file contains

@echo OFF
@set i=1
echo ENTER Your First Name :
setlocal enableDelayedExpansion
set /p input_value[%i%]=%=%
call:print
endlocal
@set i=2
setlocal enableDelayedExpansion

echo ENTER Your Last Name :
set /p input_value[%i%]=%=%
call:print
endlocal
:print
@echo !input_value[%i%]!
GOTO:EOF

Output:

D:\backup_app\bat>test.cmd
ENTER Your First Name :
radhe
radhe
ENTER Your Last Name :
kishan
kishan
!input_value[2]!

The last line in the output is what that is troubling me.

4
  • What do you expect on !input_value[2]!? Try remove endlocal above :print and you will get kishan Commented Aug 21, 2015 at 13:05
  • I don't want !input_value[2]! in the output. But I am getting it. Commented Aug 21, 2015 at 13:14
  • Then you should refer JosefZ's answer Commented Aug 21, 2015 at 13:16
  • Insert goto :EOF or exit /B before :print label because you are "falling" into the :print routine a third time unintentionally... Commented Aug 21, 2015 at 13:18

1 Answer 1

2

You need to skip over the :print procedure (an you could enable delayed expansion only to necessary code snippet) as follows:

@echo OFF
setlocal enableExtensions
@set i=1
echo ENTER Your First Name :
set /p input_value[%i%]=%=%
call:print
@set i=2

echo ENTER Your Last Name :
set /p input_value[%i%]=%=%
call:print
goto :next
:print
    setlocal enableDelayedExpansion
    echo !input_value[%i%]!
    endlocal
    GOTO:EOF
:next
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.