如何将用户输入插入增量变量中

时间:2019-06-02 14:20:10

标签: batch-file variables increment

我有此代码:

@echo off
cls
setlocal enableextensions enabledelayedexpansion

set count=0

REM Now this will work and it will display the items of my itemlist.txt in numerical order
for /F "tokens=*" %%a in ('type mydir\itemlist.txt') do (
 set /a count+=1
 echo !count!^) This is the item %%a
 set counted[!count!]=%%a
)

REM This works fine and diplays items defined by their corresponding numbers
echo %counted[1]%
echo %counted[2]%

REM And I want to do something like this:
set /p newcount=Input the number of the item of your choice:

REM Tried something like this:
set chosenitem=%counted[!newcount!]%

REM And didn't worked. The following doesn't display anything...
echo %chosenitem%
pause

所以我的问题是,我怎样才能准确地将用户选择的数量插入到%counted%变量中?

1 个答案:

答案 0 :(得分:0)

我发布帖子后立即发现:Echo batch file arrays using a variable for the index?

所以我已经测试过:

echo !counted[%newcount%]!

工作完美!