如何增加变量名中包含单词的变量?

时间:2016-02-25 14:25:03

标签: batch-file

这似乎是一个简单的解决方案,所以我要做的是每行获取.txt文件中的所有内容并将其创建为变量。 从那里我想输出一遍又一遍收集的每个变量。

示例:

Contents1
Contents2
Contents3

我目前在我的脚本中找到的其他内容是它会抓住每个并将它们存储为:

var1
var2
var3

基本上就是:

var1 = Contents1
var2 = Contents2
var3 = Contents3

然而在我的循环中,我似乎无法抓住var1,var2,var3等。 我正在尝试增加每个var,因为每个文件中有超过100个变量。

@echo off

REM Enable Delayed Expansion for !Variables!
setlocal EnableDelayedExpansion

REM Get file contents and store them per variable ex: var1, var2, var3
set VarList=0
for /F "tokens=*" %%A in (input.txt) do (
    SET /A VarList=!VarList! + 1
)
set var

SET TierLevel=Common
SET Type=Armor
SET EnchantName=%TierLevel%_%Type%

set Counter=0

REM For this, there would be 3 variable outputs per body for each enchantment
REM However the lower the tier, the less there will be.
REM Minimum 1 & Max 3
set /A Amount=%VarList%/3


:repeat
REM Use all the variables [With the 3 per body] until each type is filled
REM Final output should be something such as:
REM Common_Armor1 -> Common_Armor5
REM Within Common_Armor1 it will contain:
REM Sharpness, Fireaspect, and Smite
REM Within Common_Armor2 it will contain:
REM Sharpness, Fireaspect and Looting

set /A INeedThisToIncrement+=%var%1

IF "!Amount!"=="!Counter!" (
    Echo All complete!
    pause
    exit
        ) else (
echo      %EnchantName%%Counter%: >> output.txt
echo          %INeedThisToIncrement% >> output.txt
        )
        set /A Counter=Counter+1
        goto :repeat

我被困的部分是......

我需要变量:INeedThisToIncrement 为var1,然后增加输出变量:var2,var3,var4,var5等。

因此每次都需要增加。 我试图创建一些相对于var + %% a或类​​似的东西,但是它会输出名称var +要递增的数字。

它通过控制台输出:

VarList=9

All complete

Press any key to continue . . .

output.txt包含:

 Common_Armor0: 
     1 
 Common_Armor1: 
     2 
 Common_Armor2: 
     3 

有没有人知道如何增加var1所以每个循环都会输出var2,var3,var4等?

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

@echo off

REM Enable Delayed Expansion for !Variables!
setlocal EnableDelayedExpansion

REM Get file contents and store them per variable ex: var1, var2, var3
set VarList=0
for /F "tokens=*" %%A in (input.txt) do (
    SET /A VarList=!VarList! + 1
    set "var!VarList!=%%A"
)
set var
pause

然后在你的重复循环中

set "INeedThisToIncrement+=!var%counter%!"

从1开始你的计数器