none
Comparing mutiple files

    Question

  • Hello everyone. I am back and hope you all are doing well.

    I am creating a visual basic program to run a file comparison. I have the files loaded and edited as needed, but stuck on how to run the loop I need. 

    I have loaded in a textbox my files as shown below.

    C:\Compare\rpt\101_VersionInfo_RPT.csv
    C:\Compare\rpt\104_VersionInfo_RPT.csv
    C:\Compare\rpt\105_VersionInfo_RPT.csv
    C:\Compare\rpt\108_VersionInfo_RPT.csv
    C:\Compare\rpt\110_VersionInfo_RPT.csv
    C:\Compare\rpt\111_VersionInfo_RPT.csv

    I need to run 

    process.start(fc /C /L /N C:\Compare\rpt\101_VersionInfo_rpt.csv C:\Compare\rpt\104\VersionInfo_rpt.csv > C:\Compare\rpt\Results\v101_rpt.txt)

    On every file. Meaning File 101 needs to be compared with files 104, 105, 108, 110, and 111. 

    Then Files 105 needs to be compared with files 108, 110, and 111, etc..

    Repeated until all files are done. I have hundreds of files. I do not have a code for this part, but have code to fix the files.

    Here is the code I have.

    Dim content As String
    Dim Temp As String
    Dim PostPlant As String = ""
        
            '### Edits all rpt.csv files to remove plant ID for comparison
    
            For Each FilePath As String In IO.Directory.GetFiles("C:\Compare\rpt")
                Dim FileEXE As String = IO.Path.GetExtension(FilePath)
    
                If (FileEXE = "csv") Or (FileEXE = "CSV") _
                    Or (FileEXE = ".csv") Or (FileEXE = ".CSV") Then
    
                    txtFiles.AppendText(FilePath & vbCrLf)
                    txtLog.AppendText("Loaded " & FilePath & vbCrLf)
    
                    txtPlantID.AppendText(FilePath.Substring(15, 3) & vbCrLf)
                    txtTemp.Text = FilePath.Substring(15, 3)
                    Temp = "_" & FilePath.Substring(15, 3)
                    PostPlant = FilePath.Substring(15, 3)
                    txtLog.AppendText("Split Plant ID " & FilePath.Substring(15, 3) & vbCrLf)
    
                    content = IO.File.ReadAllText(FilePath)
                    txtLog.AppendText("Read File " & FilePath)
    
                    content = content.Replace(Temp, "_XXX")
                    txtLog.AppendText(" Replaced _" & Temp & " with _XXX" & vbCrLf)
    
                    IO.File.WriteAllText(FilePath, content)
                    txtLog.AppendText("Written File " & FilePath & vbCrLf)
    
                End If
            Next

    Thursday, December 07, 2017 4:59 PM

All replies

  • What need to be compared, the size? And what has to be edited and how. 

    Not in code but in words. 


    Success
    Cor

    Thursday, December 07, 2017 5:03 PM
  • Inside the files are paths, files, versions, and dates.

    So in the end we are making sure that file "System.dll" in "C:\Windows\" has the same date and version.

    The files are .csv

    If not we need to report them. 

    I just started here and they use the fc.exe to do this, but it is very tedious.

    Thursday, December 07, 2017 5:05 PM
  • That is just an example. Each csv file can list hundreds of records.
    Thursday, December 07, 2017 5:06 PM
  • You can use FC in a process 

    Be aware that comparing text files seems easy, but if you do it once in Word you will see that it is quit complex. 

    You can use a process like FC like this. 

    http://www.vb-tips.com/Shell.aspx


    Success
    Cor

    Thursday, December 07, 2017 5:24 PM
  • Not to make it sound as though this is a trivial task and that the following would be your solution as the following would only (and that is a maybe) be for line content that was not complex. It's in C# yet the VB.NET code is shown below.

    Dim directory As String = "C:\Whatever\"
    Dim linesA() As String = File.ReadAllLines(Path.Combine(directory, "FileA-Database.txt"))
    Dim linesB() As String = File.ReadAllLines(Path.Combine(directory, "FileB-Database.txt"))
    
    Dim onlyB As IEnumerable(Of String) = linesB.Except(linesA)
    
    File.WriteAllLines(Path.Combine(directory, "Result.txt"), onlyB)
    For me "Inside the files are paths, files, versions, and dates." could mean more than simply comparing lines or maybe that is good enough ???


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 07, 2017 5:36 PM
    Moderator
  • Hi

    Here is some code that compares files with each other in a given path. It lists the results, whether or not they are the same,  in a ListBox1 on Form1.

    If you show examples of the text that need to be compared, and what information you need from the comparison, then this code can possibly be amended as needed.

    I used the same file names as per your post, but in a different path.

    ' Form1 with ListBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim fold As String = "C:\Users\lesha\Desktop\Plans\New folder\New folder"
        Dim lst As New List(Of String)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim files As List(Of String) = My.Computer.FileSystem.GetFiles(fold, FileIO.SearchOption.SearchTopLevelOnly, "*.csv").ToList
    
            Dim count As Integer = files.Count - 1
            For f As Integer = 0 To count
                For f2 As Integer = f + 1 To count
    
                    If f < count Then
                        Dim same As Boolean = FileCompare(files(f), files(f2))
    
                        If same Then
                            ListBox1.Items.Add("File " & f.ToString & " is same as File " & f2.ToString)
                        Else
                            ListBox1.Items.Add("File " & f.ToString & " is NOT same as File " & f2.ToString)
                        End If
    
                    End If
                Next
            Next
    
        End Sub
        Private Function FileCompare(ByVal file1 As String, ByVal file2 As String) As Boolean
            Dim file1byte As Integer
            Dim file2byte As Integer
            Dim fs1 As IO.FileStream
            Dim fs2 As IO.FileStream
    
            ' Determine if the same file was referenced two times.
            If (file1 = file2) Then
                ' Return 0 to indicate that the files are the same.
                Return True
            End If
    
            ' Open the two files.
            fs1 = New IO.FileStream(file1, IO.FileMode.Open)
            fs2 = New IO.FileStream(file2, IO.FileMode.Open)
    
            ' Check the file sizes. If they are not the same, the files
            ' are not equal.
            If (fs1.Length <> fs2.Length) Then
                ' Close the file
                fs1.Close()
                fs2.Close()
    
                ' Return a non-zero value to indicate that the files are different.
                Return False
            End If
    
            ' Read and compare a byte from each file until either a
            ' non-matching set of bytes is found or until the end of
            ' file1 is reached.
            Do
                ' Read one byte from each file.
                file1byte = fs1.ReadByte()
                file2byte = fs2.ReadByte()
            Loop While ((file1byte = file2byte) And (file1byte <> -1))
    
            ' Close the files.
            fs1.Close()
            fs2.Close()
    
            ' Return the success of the comparison. "file1byte" is
            ' equal to "file2byte" at this point only if the files are 
            ' the same.
            Return ((file1byte - file2byte) = 0)
        End Function
    
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, December 07, 2017 5:58 PM
    Thursday, December 07, 2017 5:57 PM
  • You can use FC in a process 

    Be aware that comparing text files seems easy, but if you do it once in Word you will see that it is quit complex. 

    You can use a process like FC like this. 

    http://www.vb-tips.com/Shell.aspx


    Success
    Cor

    That I know and understand. The problem I am having is how to run the FC command on the various versions. 

    Here is what I am trying to accomplish for hundreds of files.

    fc /C /L /N C:\Compare\rpt\001_File1.csv C:\Compare\002_File2 > C:\Compare\rpt\Results\001v002_rpt.txt
    fc /C /L /N C:\Compare\rpt\001_File1.csv C:\Compare\003_File2 > C:\Compare\rpt\Results\001v003_rpt.txt
    fc /C /L /N C:\Compare\rpt\001_File1.csv C:\Compare\004_File2 > C:\Compare\rpt\Results\001v004_rpt.txt
    fc /C /L /N C:\Compare\rpt\001_File1.csv C:\Compare\005_File2 > C:\Compare\rpt\Results\001v005_rpt.txt

    fc /C /L /N C:\Compare\rpt\002_File1.csv C:\Compare\001_File2 > C:\Compare\rpt\Results\002v001_rpt.txt
    fc /C /L /N C:\Compare\rpt\002_File1.csv C:\Compare\003_File2 > C:\Compare\rpt\Results\002v003_rpt.txt
    fc /C /L /N C:\Compare\rpt\002_File1.csv C:\Compare\004_File2 > C:\Compare\rpt\Results\002v004_rpt.txt
    fc /C /L /N C:\Compare\rpt\002_File1.csv C:\Compare\005_File2 > C:\Compare\rpt\Results\002v005_rpt.txt

    fc /C /L /N C:\Compare\rpt\003_File1.csv C:\Compare\001_File2 > C:\Compare\rpt\Results\003v001_rpt.txt
    fc /C /L /N C:\Compare\rpt\003_File1.csv C:\Compare\002_File2 > C:\Compare\rpt\Results\003v002_rpt.txt
    fc /C /L /N C:\Compare\rpt\003_File1.csv C:\Compare\003_File2 > C:\Compare\rpt\Results\003v004_rpt.txt
    fc /C /L /N C:\Compare\rpt\003_File1.csv C:\Compare\005_File2 > C:\Compare\rpt\Results\003v005_rpt.txt

    Thursday, December 07, 2017 6:15 PM
  • Glenn,

    Is the goal here to compare a given file to another and know whether or not there's a difference?

    If so, then you might consider using file hashing and compare the results of the two hashes.

    If that's applicable, I'll show you how to do that (if you want).


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, December 07, 2017 7:44 PM
  • I don't know what the question is.

    An alternative might be to (temporarily) load each CSV file into a database with a field in the table indicating what file the data is from. Then use the database table to do the comparison.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, December 07, 2017 8:39 PM
  • Glenn,

    Is the goal here to compare a given file to another and know whether or not there's a difference?

    If so, then you might consider using file hashing and compare the results of the two hashes.

    If that's applicable, I'll show you how to do that (if you want).


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Yes. Each <g class="gr_ gr_4 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="4" id="4">csv</g> file contains information like 

    Path,File,Type,Date,Version
    D:\pcdata\Bin,BarcodeParserVariable.dll,<g class="gr_ gr_45 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="45" id="45">dll</g>,7/13/2017,1.0.0.2
    D:\pcdata\Bin,ClassLibraryKitting.dll,<g class="gr_ gr_46 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="46" id="46">dll</g>,7/13/2017,1.0.0.49

    So I did put the files into an MS-Access file and <g class="gr_ gr_124 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="124" id="124">created</g> reports which the company likes, however they want me to run the FC command prompt to compare the files as well.

    This is so we can compare the FC results and my MS-Access report.

    Thursday, December 07, 2017 9:55 PM

  • Yes. Each <g class="gr_ gr_4 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="4" id="4">csv</g> file contains information like 

    Path,File,Type,Date,Version
    D:\pcdata\Bin,BarcodeParserVariable.dll,<g class="gr_ gr_45 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="45" id="45">dll</g>,7/13/2017,1.0.0.2
    D:\pcdata\Bin,ClassLibraryKitting.dll,<g class="gr_ gr_46 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="46" id="46">dll</g>,7/13/2017,1.0.0.49

    So I did put the files into an MS-Access file and <g class="gr_ gr_124 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="124" id="124">created</g> reports which the company likes, however they want me to run the FC command prompt to compare the files as well.

    This is so we can compare the FC results and my MS-Access report.

    Uhh ... want to try that again please?

    Don't copy/paste - that's what usually causes it but the forum is having its own set of problems too.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, December 07, 2017 9:57 PM
  • This is so we can compare the FC results and my MS-Access report.

    So they don't trust the report.

    I have another suggestion. You could create a program that generates a batch file (file extension bat or cmd) that executes the FC command. Since you would be using the actual FC command they can't complain.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, December 07, 2017 10:30 PM
  • Inside the files are paths, files, versions, and dates.

    So in the end we are making sure that file "System.dll" in "C:\Windows\" has the same date and version.

    The files are .csv

    If not we need to report them. 

    I just started here and they use the fc.exe to do this, but it is very tedious.

    Hi Broggy69,

    You said that you want to compare many .csv files, I think you can take a look the following link:

    https://stackoverflow.com/questions/28834363/file-comparison-in-vb-net

    But you also said that we are making sure that file "System.dll" in "C:\Windows\" has the same date and version. I am not sure what you want to do, can you please describe more detailed info here?

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 08, 2017 7:42 AM
    Moderator