[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Append Text To text file

Posted on 2004-10-13
2
Medium Priority
?
205 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 1000 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

650 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