Solved

# Add line number to text file

Posted on 2013-06-17
528 Views
I have a text file without line numbers and i need to insert a line number for each file.

Text File

aaaaaa
bbbbb
cccccc
.....

With line numbers

1 aaaaaa
2 bbbbb
3 cccccc
0
Question by:rflorencio

LVL 42

Assisted Solution

sedgwick earned 150 total points
ID: 39253353
        Dim lines = File.ReadAllLines("c:\temp\1.txt")
Dim list As List(Of String) = New List(Of String)
For Each l In lines
list.Add((list.Count + 1).ToString & " " & l)
Next

File.WriteAllLines("c:\temp\1.txt", list.ToArray)

0

LVL 75

Accepted Solution

käµfm³d   👽 earned 350 total points
ID: 39253398
Depending on how large your files are, it may be more prudent to use StreamReaders and StreamWriters.

e.g.

Imports System.IO

...

Using writer As New StreamWriter("C:\path\to\temp\file.txt")
Dim i As Integer = 1

i += 1
End While
End Using
End Using


You can move (i.e. rename) the temp file to the original file to effectively overwrite the old file.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Help with solving error related to resources file 4 45
scanning dentists xray (the small ones) 3 53
VB.net Open Folder in Windows Explorer 3 24
Store results in vb.net 3 20
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…