windowsbatch-file

How to add line numbers to a text file from a batch file (Windows)


How would I be able to add line numbers to a text file from a batch file / command prompt?

e.g.

1 line1
2 line2
etc

Solution

  • Here you go:

    @echo off
    FOR /L %%G IN (1, 1, 100) DO (
         echo %%G line%%G
    )
    

    This will probably only work in a batch file and not on the command line.

    For more info, see this page.

    If you want to loop over an existing file and add numbers to it, you'd have to process the file with a for /F loop instead, and within each loop iteration use a statement like set /a counter+=1 to increment your counter. Then spit out each line to a new file and finally replace the old file with the new one.