1

This is what I want to do, a batch that reads a file (e.g. file.txt) and output line# + token.

This is what I tried to do (which obviously didn't work):

set count=0
set InputFile=file.txt
for /f "tokens=1-3 delims=," %%A IN (%InputFile%) DO (
    set /a count+=1
    echo %count%. %%A
)

file.txt contains:

something,else,
something1,else1, 
something2,else2, 
something3,else3,
etc.

What I would like to output, is:

1. something
2. something1
3. something2
etc.

What this code is actually throwing in the output:

0. someting
0. something1
0. something2
etc

Any ideas?

Laf
  • 7,965
  • 4
  • 37
  • 52
  • possible duplicate of [Batch file variable scope issue](http://stackoverflow.com/questions/21387098/batch-file-variable-scope-issue) – Laf Jan 28 '14 at 19:40
  • I have flagged your question as being a duplicate (see automatic comment above). The answer I provided applies to your case, and the explanation in MC ND's answer will tell you exactly what is happening with your batch file. – Laf Jan 28 '14 at 19:41
  • I don't get it, I tried adding setlocal EnableDelayedExpansion but it didn't change anything. Are you sure that the answer provided applies? – user3246010 Jan 28 '14 at 19:50
  • Did you also change how you refer to your variable from `%count%` to `!count!`? – Laf Jan 28 '14 at 19:53
  • Just saw the problem. Using setlocal EnableDelayedExpansion plus !count! instead of %count% solved. THANKS!! – user3246010 Jan 28 '14 at 19:54

1 Answers1

2

You need to add SETLOCAL ENABLEDELAYEDEXPANSION before you FOR loop.

Then change echo %count%. %%A to echo !count!. %%A.

aphoria
  • 19,796
  • 7
  • 64
  • 73
  • No. The `set /a` command correctly takes the value even if it is not enclosed in exclamation marks. However, they _are_ required in the `echo !count!. %%A` – Aacini Jan 28 '14 at 20:34