Remove Lines From Notepad and Write Data in New New Notepad With Group Key Repeated With Each Lines

Amit kumar Yadava 81 Reputation points
2023-03-24T06:43:10.2233333+00:00

Good Morning Great Minds !

I need Help to read Data From Notepad where a specific string(Group Key) repeated each page. I need To write Group key with each line of strings.

in OriginalFile.txt i Removed unwanted Strings and Make a new file after remove unwanted strings in File Name ConvertedFile.txt.but i need to write these to Like .OriginalFile.txt

My Boss Ask me these data in excel look like DesireFormat.PNG. My problem is A specific string which are repeating on top and i am helpless to write these string in list with loop each line,Please help me

The Files Attached Here. My code Are :


 Private Sub Btntb2Browse_Click(sender As System.Object, e As System.EventArgs) Handles Btntb2Browse.Click
        Dim Ofd1 As New OpenFileDialog
        With Ofd1
            .Filter = "Text Files|*.txt"
        End With
        If Ofd1.ShowDialog = DialogResult.OK Then
            TxtTb2FilePath.Text = Ofd1.FileName
        End If
    End Sub

    Private Sub BtnTb2Read_Click(sender As System.Object, e As System.EventArgs) Handles BtnTb2Read.Click
        Dim AllLines As List(Of String) = File.ReadAllLines(TxtTb2FilePath.Text).ToList()
        Dim Stvill As String = ""
        For index As Integer = AllLines.Count - 1 To 0 Step -1
            If AllLines(index).Contains("Standing") Or AllLines(index).Contains("Society:") Or AllLines(index).Contains("Centre:") Or AllLines(index).Contains("=") Or AllLines(index).Contains("-") Or AllLines(index).Contains("") Or AllLines(index).Contains("Village Total") Or AllLines(index).Contains("Grand Total") Then
                AllLines.RemoveAt(index)
            End If
        Next
        File.WriteAllLines(IO.Path.Combine(Application.StartupPath, "Converted.txt"), AllLines)
        Process.Start(IO.Path.Combine(Application.StartupPath, "Converted.txt"))
    End Sub

The Output should be like

DesireFormat

I will be always Highly obliged to you .

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,363 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 26,426 Reputation points Microsoft Vendor
    2023-03-24T09:03:26.67+00:00

    Hi @Amit kumar Yadava ,

    You can refer to the following code.

            Dim AllLines As List(Of String) = File.ReadAllLines(TxtTb2FilePath.Text).ToList()
            Console.WriteLine(AllLines.Count)
            Dim Stvill As String = ""
            For index As Integer = AllLines.Count - 1 To 0 Step -1
                If AllLines(index).Contains("Standing") Or AllLines(index).Contains("Society:") Or AllLines(index).Contains("Centre:") Or AllLines(index).Contains("=") Or AllLines(index).Contains("-") Or AllLines(index).Contains("") Or AllLines(index).Contains("Village Total") Or AllLines(index).Contains("Grand Total") Then
                    AllLines.RemoveAt(index)
                End If
    
            Next
    
            Dim village As String = ""
            For index As Integer = 0 To AllLines.Count - 1 Step 1
                If AllLines(index).Contains("Village") Then
                    village = AllLines(index).Split(" ")(2)
                ElseIf AllLines(index).Length > 9 Then
                    AllLines(index) = AllLines(index).Insert(9, vbTab & village)
                End If
            Next
            File.WriteAllLines(IO.Path.Combine(Application.StartupPath, "Converted.txt"), AllLines)
            Process.Start(IO.Path.Combine(Application.StartupPath, "Converted.txt"))
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful