none
copy line from textfile to another RRS feed

  • Question

  • I have two text files
    I want to move the non-existent lines with the first file from the second file

    1 text file content

    - good

    - bad

    2 text file content

    - good

    - bad

    - Excellent

    1 text file content after code add

    - good

    - bad

    -Excellent   'add  this lines or any lines not found in firist text file

    Friday, February 16, 2018 11:22 PM

Answers

  • Hi

    Change  the .ReadLines    to    .ReadAllLines    in both occurrences.

    Also, change the last line to

    IO.File.WriteAllLines(holdFilename, file1.ToArray)


    Regards Les, Livingston, Scotland

    • Marked as answer by monemas Saturday, February 17, 2018 6:23 PM
    Saturday, February 17, 2018 12:37 AM

All replies

  • I want to move the non-existent lines with the first file from the second file

    You would start by setting out the steps you need to go through. Some of these might be trivial - some might be complex.  That will depend on the detail of the application, which you haven't specified.

    Get the file path of the first file
    Get the file path of the second file
    Read all the lines of the first file
    For each line in the second file:
      If this line does not exist in the list of lines from the first file then add this line to the list of lines (at a specific position?)
    When all lines processed, Delete the first file 
      Delete the first file
      Create a new file named as for the first file and write out all the lines.

    You should show the code you already have for this procedure, and indicate what is the problem you have run into.

     

    Friday, February 16, 2018 11:55 PM
  • Hi

    You would need to supply better information if you want a better answer than this one. For example does upper/lower case matter, is it whole lines or partial etc.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim file1 As List(Of String) = Nothing
        Dim file2 As List(Of String) = Nothing
        Dim holdFilename As String = Nothing
        Dim fb As New OpenFileDialog
        If fb.ShowDialog = DialogResult.OK Then
          holdFilename = fb.FileName
          file1 = IO.File.ReadAllLines(holdFilename).ToList
        End If
        If fb.ShowDialog = DialogResult.OK Then
          file2 = IO.File.ReadAllLines(fb.FileName).ToList
        End If
        For Each s As String In file2
          If Not file1.Contains(s) Then file1.Add(s)
        Next
        IO.File.WriteAllLines(holdFilename, file1.ToArray)
      End Sub
    End Class


    Regards Les, Livingston, Scotland






    • Edited by leshay Saturday, February 17, 2018 8:41 PM Amended code
    Saturday, February 17, 2018 12:02 AM
  • Hi

    You would need to supply better information if you want a better answer than this one. For example does upper/lower case matter, is it whole lines or partial etc.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim file1 As List(Of String) = Nothing
        Dim file2 As List(Of String) = Nothing
        Dim holdFilename As String = Nothing
        Dim fb As New OpenFileDialog
        If fb.ShowDialog = DialogResult.OK Then
          holdFilename = fb.FileName
          file1 = IO.File.ReadLines(holdFilename).ToList
        End If
        If fb.ShowDialog = DialogResult.OK Then
          file2 = IO.File.ReadLines(fb.FileName).ToList
        End If
        For Each s As String In file2
          If Not file1.Contains(s) Then file1.Add(s)
        Next
        IO.File.WriteAllLines(holdFilename, file1)
      End Sub
    End Class


    Regards Les, Livingston, Scotland





    i use vb2010 frimwork 3.5

    get this error

    Error    1    'ReadLines' is not a member of 'System.IO.File'. 

    Error    2   Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to '1-dimensional array of String'. 
      

    Saturday, February 17, 2018 12:19 AM
  • Hi

    Change  the .ReadLines    to    .ReadAllLines    in both occurrences.

    Also, change the last line to

    IO.File.WriteAllLines(holdFilename, file1.ToArray)


    Regards Les, Livingston, Scotland

    • Marked as answer by monemas Saturday, February 17, 2018 6:23 PM
    Saturday, February 17, 2018 12:37 AM