Solved

Append Text To text file

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now