Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

Add line number to text file

Posted on 2013-06-17
Medium Priority
645 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

Meir Rivkin earned 600 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 1400 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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.