Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8386263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:53:15+00:00 2026-06-09T17:53:15+00:00

I want to extract some specific lines from a text file to other text

  • 0

I want to extract some specific lines from a text file to other text file. i am using the following code

    Imports System.IO



Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Tr As IO.TextReader = System.IO.File.OpenText("C:\Assignment.txt")
        For c As Integer = 1 To 10

            If c = 7 Then
                Dim MyFileLine As String = Split(Tr.ReadToEnd(), vbCrLf)(c) & vbCrLf
                Tr.Close()



                Dim TW As System.IO.TextWriter
                'Create a Text file and load it into the TextWriter 
                TW = System.IO.File.CreateText("C:\Assignment1.txt")
                TW.WriteLine(MyFileLine)
                'Flush the text to the file 
                TW.Flush()
                'Close the File 
                TW.Close()
            End If

        Next c
    End Sub
End Class

But this code extract only the line no 7 where i want to extract the 8th,9th,10th,14th,15th,16th, lines also . Please guide me the right solution. Thank u in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:53:17+00:00Added an answer on June 9, 2026 at 5:53 pm

    There seems to be several issues here. I will correct them and then explain below:

    Imports System.IO
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim currentLine As String
            Dim lineCounter As Integer = 1
            Dim lineNumbersRequired As List(Of Integer) = New List(Of Integer)
            lineNumbersRequired.Add(7)
            lineNumbersRequired.Add(8)
            lineNumbersRequired.Add(9)
            lineNumbersRequired.Add(10)
            lineNumbersRequired.Add(14)
            lineNumbersRequired.Add(15)
            lineNumbersRequired.Add(16)
    
            Dim TW As System.IO.TextWriter
            'Create a Text file and load it into the TextWriter 
            TW = System.IO.File.CreateText("C:\Assignment1.txt")
    
            Using Tr As IO.TextReader = New IO.StreamReader("C:\Assignment.txt")
                While Not Tr.EndOfStream
                    If lineNumbersRequired.Contains(lineCounter) Then
                        Dim MyFileLine As String = Split(currentLine, vbCrLf)(c) & vbCrLf
                        TW.WriteLine(MyFileLine)
                    End If
                    lineCounter = lineCounter + 1
                End While
            End Using
    
            TW.Flush()
            'Close the File 
            TW.Close()
    
        End Sub
    End Class
    

    NOTE: Code not tested, but should be pretty close if you do get a few compile errors!

    Ok then, just a quick rundown of what I did here:

    1. Changed the For Loop into a while because you had the for loop running from 1 To 10, so even if it worked, then you would have never read past the 10th line in your file. So I have changed it to a while loop that will end when the TextReader has read all lines in the file. Also the current line read from the file has been added to a new variable called currentLine.
    2. The new currentLine variable is now used to populate the lines of your writing file.
    3. I have added a list of Integers that will hold the line numbers you want to keep, then within the while loop I have a counter that counts each line as it is processed and if this counter is inside the list of line numbers you want to save into your output file, then it will output the current line.

    Let me know how you get on, and if you need more of an explanation on any of it then please ask.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to python. I want to extract some text from the CNN website.
I want to extract images from an PDF. I'm using iTextSharp right now. Some
Background - I want to extract specific columns from a csv file. The csv
I want to use ADO.net to extract some data from an Excel file. This
I want to extract some phone numbers that are in a local text file
I want to extract some links from a XML document (links are in same
i want to extract some text in certain website. here is web address what
I want to extract some numbers from the input line which is a string.
I want to extract out some specified columns from the matrix. My matrix being
I have some html files and want to extract only lines with containing these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.