Home > database >  How to reference the value of an environment variable with using another environment variable value
How to reference the value of an environment variable with using another environment variable value

Time:07-08

I know that similar questions have been asked before and I have seen them but neither does !var[%Z%]! nor %var[!Z!]% work here:

@echo off
set Z=0
setlocal enabledelayedexpansion
set count=0
for /f "tokens=*" %%x in (Data) do (
    set /a count =1
    set var[!count!]=%%x
)
:end
cls 
echo %var[!Z!]%
choice  /N /C QE
IF %errorlevel% == 1 GOTO ZP
IF %errorlevel% == 2 GOTO ZM
pause >nul
goto :end

:ZP
set /a Z=%Z% 1
goto :end
:ZM
set /a Z=%Z%-1
goto :end

I tried them and they don't work. What can I do?

Data is a file with following lines:

A
B
C
D
E
F
AA
AB
AC
AD

CodePudding user response:

I'm assuming that this was your intention, (small modifications included). i.e. to be able to two way cycle through the data items using the Q and E keys.

Code as per the suggestion in my comment:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set /A "Z=count=0"

Setlocal EnableDelayedExpansion
For /F "Tokens=* UseBackQ" %%G In ("Data.ext") Do (
    Set /A count  = 1
    Set "var[!count!]=%%G"
)
Set "count="

:End
ClS 
Echo(!var[%Z%]!
%SystemRoot%\System32\choice.exe /C QE /N
If ErrorLevel 2 GoTo ZM
GoTo ZP
Pause 1>NUL
GoTo End

:ZP
Set /A Z  = 1
GoTo End

:ZM
Set /A Z -= 1
GoTo End

  • Related