0

How do I use a variable like %%i in a permanent variable? (I don't really know the correct terms, so I hope anyone can figure out what I mean)

This is the code I am using:

@echo off
color 0f
goto number

:number
title number
cls
echo number of options?
set /p num=
goto option

:option
for /l %%i in (1,1,%num%) do (
cls
echo Name nr. %%i
echo Enter a option
set /p n%%i=
echo %%i = %n%%i% >> log.txt
)
goto select

:select
cls
echo %n2%
pause >nul

the "%n2%" works for whatever you put in second, but when I try to print it into a file ( echo %%i = %n%%i% >> log.txt ) it doesn't work. I know the "%n%%i%" is not correct, But I don't really know what to actually put there.

  • 2
    Either use `call echo %%i = %%n%%i%% >> log.txt` or enable delayed expansion with `setlocal enabeldelayedexpansion` and change to `echo %%i = !n%%i! >> log.txt` –  Dec 11 '16 at 19:41
  • 3
    Use `!n%%i!` and `EnableDelayedExpansion`. See [this answer](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Dec 11 '16 at 19:43
  • Thank you @Aacini , it worked – Gido Selten Dec 11 '16 at 19:48
  • If the information at the linked answer aids you, I invite you to upvote such an answer. **`;)`** – Aacini Dec 12 '16 at 13:49

1 Answers1

0
:option
for /l %%i in (1,1,%num%) do (
cls
echo Name nr. %%i
echo Enter an option
set /p option%%i=
)
goto select

:select
set option>log.txt
cls
echo %option2%

You may like to consider this.

The command

set option

will show every environment variable that starts option in the format option1=Gido, which is why I changed n to option (there are other variables set that start n)

Magoo
  • 77,302
  • 8
  • 62
  • 84