Solved

Append Text To text file

Posted on 2004-10-13
2
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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