user3246010
user3246010

Reputation: 73

Batch for reading a file and output line number + token?

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?

Upvotes: 1

Views: 72

Answers (1)

aphoria
aphoria

Reputation: 20199

You need to add SETLOCAL ENABLEDELAYEDEXPANSION before you FOR loop.

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

Upvotes: 2

Related Questions