Solved

Append Text To text file

Posted on 2004-10-13
2
199 Views
Last Modified: 2010-04-24
Hello.

I have a text file that changes each time a program is run. The text file is call log.txt which is the output of "dir /B > log.txt" in a directory call C:\scans\ . An example of the results could be :

BELLE-16051KTX
BELLE-16053KTX
BELLE-16054KTX
BELLE-16056KTX
CG-76666BDTX
CG-76663BDTX
CG-76665BDTX
CG-76664BDTX
OA-2752ACO
OA-2751ACO
OA-2756ACO
OA-2755ACO
OA-2754ACO

What i need to do is append the text file so it looks like this:


1. BELLE-16051KTX
2. BELLE-16053KTX
3. BELLE-16054KTX
4. BELLE-16056KTX
1. CG-76666BDTX
2. CG-76663BDTX
3. CG-76665BDTX
4. CG-76664BDTX
1. OA-2752ACO
2. OA-2751ACO
3. OA-2756ACO
4. OA-2755ACO
5. OA-2754ACO

If this is too hard, an alternate solution would be to :
Count occurances of the 3 strings ie how many "OA-" , "BELLE-" and "CG-" occur in the file and list a total at the bottom
like this:
OA- count = 5 BELLE- count = 4 CG- count = 4

Which ever is easier.
0
Comment
Question by:jerrycsakanyjr
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 12300941
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

'  Windows Form Designer generated code

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fileName As String = "C:\scans\log.txt"
        Dim tempFileName As String = "C:\scans\tempLog.tmp"

        If Not File.Exists(fileName) Then
            MsgBox(fileName, MsgBoxStyle.Critical, "File Not Found")
            Exit Sub
        End If

        Dim sr As StreamReader = New StreamReader(fileName)
        Dim sw As StreamWriter = New StreamWriter(tempFileName)
        Dim inputLine As String
        Dim dashPos As Integer
        Dim prefix As String
        Dim lastPrefix As String
        Dim lastPrefixCount As String

        Do
            inputLine = sr.ReadLine
            If Not (inputLine Is Nothing) Then
                dashPos = inputLine.IndexOf("-")
                If dashPos > 0 Then
                    prefix = inputLine.Substring(0, dashPos)
                    If Not prefix.Equals(lastPrefix) Then
                        lastPrefixCount = 1
                        lastPrefix = prefix
                    Else
                        lastPrefixCount = lastPrefixCount + 1
                    End If
                    sw.WriteLine(lastPrefixCount.ToString & ". " & inputLine)
                End If
            End If
        Loop While Not (inputLine Is Nothing)
        sw.Close()
        sr.Close()

        File.Delete(fileName)
        File.Move(tempFileName, fileName)

        MsgBox("Done")
    End Sub

End Class
0
 
LVL 8

Assisted Solution

by:bramsquad
bramsquad earned 250 total points
ID: 12300991
here is one to add the totals at the end

    Public Function AppendTotals(ByVal PathName As String)
        Dim OA_count, BELLE_count, CG_count As Integer

        Dim reader As New StreamReader(PathName)
        Dim strReadLine As String

        If Not IsNothing(reader) Then
            strReadLine = reader.ReadLine
            Do While Not strReadLine Is Nothing
                If Mid(strReadLine, 1, 2) = "CG" Then CG_count += 1
                If Mid(strReadLine, 1, 2) = "OA" Then OA_count += 1
                If Mid(strReadLine, 1, 2) = "BE" Then BELLE_count += 1
                strReadLine = reader.ReadLine
            Loop
        End If

        reader.Close()
        reader = Nothing
        Dim writer As New StreamWriter(PathName, True)
        writer.Write(vbCrLf + vbCrLf + "Totals: " + vbCrLf + vbCrLf + "CG - " + CStr(CG_count) + vbCrLf + "OA - " + CStr(OA_count) + vbCrLf + "BELLE - " + CStr(BELLE_count))
        writer.Flush()
        writer.Close()
        writer = Nothing
    End Function
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

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

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

Join & Ask a Question