如何为此添加递增编号对于每个批处理脚本?

时间:2017-11-06 17:14:50

标签: batch-file foreach

在下面的脚本中,我试图将文本文件的内容设置为朋友号码。问题是%numb%没有像我想象的那样递增。知道怎么做吗?我尝试过研究但是我不太了解For Each脚本知道如何在脚本中同时增加一个数字和一个增量数字。谢谢!

@echo off
set numb=1
for /F "tokens=*" %%A in  (list.txt) do  (
   echo Doing junk with %%A and setting it as friend %numb%
   set Friend%numb%=%%A
    set /a numb=%numb%+1
)
set /a %numb%=%numb%-1   
echo there are %numb% Friends
pause

1 个答案:

答案 0 :(得分:1)

您需要delayed expansion 我也改变了你的计数器并实现了另一种递增方法。

@echo off
setlocal enabledelayedexpansion
set numb=0
for /F "tokens=*" %%A in  (list.txt) do  (
   set /a numb+=1
   echo Doing junk with %%A and setting it as friend %numb%
   set Friend!numb!=%%A
)
echo there are %numb% Friends:
pause