manipulate text files (1 Viewer)

pb21

Registered User.
Local time
Today, 02:37
Joined
Nov 2, 2004
Messages
122
Hi

I have a tedious task each month of creating a text file made up from data of other text files with the first and last line of each text file removed.

What method exists to iterate through each of the text files in a directory, remove the first and last row in each file then against each remaining row add a number and append whats in between to the new file. That way I end up with one file with all the relevant data.

Your help would be very much appreciated.

regards
Peter
 
Last edited:

skea

Registered User.
Local time
Today, 04:37
Joined
Dec 21, 2004
Messages
342
Hoping that you are using 2005.This is quick and dirty but should get you going.
There is another method of using a streamReader.

Code:
Imports System.IO
Imports System.Environment
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fileNames As String() = Directory.GetFiles("C:\", "*.txt")
        For index As Integer = 0 To fileNames.GetUpperBound(0) Step 1
            Dim strFilePath As String = Path.GetFullPath(fileNames(index))
            fileNames(index) = Path.GetFileNameWithoutExtension(fileNames(index))
            If Path.GetFileNameWithoutExtension(Path.GetFullPath(fileNames(index))) = "Text1" Then
                Dim Lines() As String = File.ReadAllLines(strFilePath)
                For i As Integer = 2 To Lines.GetUpperBound(0) - 1
                    File.AppendAllText("C:\Text2.txt", Lines(i) & NewLine)
                Next i
                File.AppendAllText("C:\Text2.txt", "MyNumber" & NewLine)
            End If
        Next index
    End Sub
End Class
 

pb21

Registered User.
Local time
Today, 02:37
Joined
Nov 2, 2004
Messages
122
Thats great thyank you, I did experiment over the weekend and came up with :

Dim txtfile As String
Dim sw As New StreamWriter("d:\output\amended.txt")
Dim di As New DirectoryInfo("d:\test")

' Create an array representing the files in the current directory.
Dim fi As FileInfo() = di.GetFiles("*.txt")

txtfiles.AppendText("The following files exist in the current directory:")
txtfiles.AppendText(" ")
' Print out the names of the files in the current directory.
Dim fiTemp As FileInfo

For Each fiTemp In fi
Dim filename As String
'get the file name so we can tag it onto the end of each line.
filename = fiTemp.Name
Dim Sr As New StreamReader("d:\test\" & fiTemp.Name)
txtfiles.AppendText(fiTemp.Name)
txtfiles.AppendText(", ")
Do While Sr.Peek >= 0
If (Microsoft.VisualBasic.Left(Sr.ReadLine, 5) = "Event") Then
sw.WriteLine(Microsoft.VisualBasic.Replace(Sr.ReadLine, "Event:", "") & "," & """" & filename & """")
Else
'if line starts with date or word summary dont copy to new text file

End If
Loop
Next fiTemp


HOWEVER it does not work properly. My code copies lines across with the word summary in them.

so if the line starts with the word EVent then copy to the new file but dont copy any line starting with the word summary or word date.


Any ideas how to do this OR how to delete an entire line in a text file that meets the condition.

regards In advance
 

skea

Registered User.
Local time
Today, 04:37
Joined
Dec 21, 2004
Messages
342
Use the Boolean Function StartsWith, or you can use substring to get the Words to use as criteria to exclude the corresponding lines.
You can also Adapt the code Below.
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Cursor = Cursors.WaitCursor
        Dim strLine As String
        'Get the File Names in the Directory into an array
        Dim fileName As String() = Directory.GetFiles("C:\", "*.txt")
        'Define the file you are gonna write to
        Dim sw As New StreamWriter("D:\TextMe.txt")
        'Loop Through the File Names in the Directory
        For index As Integer = 0 To fileName.GetUpperBound(0) Step 1
            'Define the file you are gonna read from
            Dim sr As New StreamReader(fileName(index))
            'get the file name so we can tag it onto the end of each line.
            While Not (sr.Peek = -1)
                'Read File In Index Line By Line
                strLine = sr.ReadLine()
                'If it starts with Date or Word, Ignore It Else Write To OutPut File
                If Not (strLine.StartsWith("Date") OrElse strLine.StartsWith("Word")) Then
                    sw.WriteLine(strLine & " From File-->" & fileName(index))
                End If
            End While
            'Write New Line After Data From Each Text File
            sw.WriteLine(NewLine)
        Next index
        'Close and Free the Garbage Collector
        sw.Close()
        GC.Collect()
        Cursor = Cursors.Default
    End Sub
 

Users who are viewing this thread

Top Bottom