If you want to fully achieve the effect the owner wants, personally, I think it is best to enable extended environment variables and then set automatic increment in the for to achieve the effect; in addition, you can directly use the if statement in the for to implement, the disadvantage is that the code is relatively redundant and the readability is not very good; in addition, you can use goto loop, each time use set /p var=<a.txt to read, and then filter out the read part in a.txt, the disadvantage is that the execution efficiency is very low and the fault tolerance is very poor.
In this way, it seems that it will take some effort to achieve the owner's purpose, but it is not the case. If you are proficient in various commands, you can also achieve the purpose flexibly by using the characteristics of various commands. For example, the findstr command and the find command have a /n parameter, which can add line numbers in front of each line of the displayed search results. Using this line number, maybe we can achieve our requirements. The sample code is as follows:
for /f "delims=: tokens=1,2" %i in ('"findstr /n . a.txt"') do set %i=%j
Note that when running the above code in a batch file, you need to change %i to %%i and %j to %%j first.
Key points explanation:
1. The line number format displayed by findstr is " line number: content ", and the format of find is " content", so use colon ":" to separate in delims.
2. The English half-width period "." after findstr matches all characters in a.txt.
3. If you need to define variables to a, you can add a sentence set a=%1% later, and so on.