Solved

Append Text To text file

Posted on 2004-10-13
2
198 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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