0

I'm new to Windows batch.

I'm trying to read lines from a file input.txt and print run<increment-value> thus for the first line it should print run1 and for the second line run2 and so on.

Below is what I tried:

set /a c=1
@echo off
for /f "tokens=*" %%a in (index.txt) do (
set /a c=c+1
echo incvalue=run%c%
echo line=%%a
)
pause

In the output, I see the line= prints each line which is what I want but the incremental value of the loop always prints run1 i.e does not increment.

Can you please suggest?

Ashar
  • 2,942
  • 10
  • 58
  • 122
  • 1
    Two things, **1.** You're not incrementing the value of the variable named `c` within your loop, you defined it with a value of `1` before the loop, and made no changes to that in your script. **2.** In order to increment it in the loop you'll want to delay variable expansion. – Compo Jun 01 '20 at 09:17
  • Sorry @Compo i did increment the value of `c`. I updated my original post. Please check. – Ashar Jun 01 '20 at 09:26
  • @Ashar i see no reason of not simplifying it to `set/ac+=1` – ScriptKidd Jun 01 '20 at 10:37
  • Depending upon the content of `index.txt`, there may be no reason to use `set` or delay expansion at all! `@(For /F "Tokens=1,* Delims=[]" %%G In ('%__AppDir__%find.exe /N /V "" ^< "index.txt"') Do @(Echo incvalue=%%G& Echo line=%%H))&Pause` – Compo Jun 01 '20 at 10:49

0 Answers0