Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to compare 2 different text file lines and then process information if a match is in both file lines - Please help

How to compare 2 different text file lines and then process information if a match is in both file lines - Please help

Scheduled Pinned Locked Moved Visual Basic
helptutorialdata-structuresregex
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11833437
    wrote on last edited by
    #1

    I am attempting to build a command line tool that will create shortcuts for an application on the start menu and desktop. I have existing code that I use elsewhere to create the shortcuts. However, the program looks at two different files one file has only the site code and the other has the other information needed to created the shortcut by site for a whole region. The idea is to go through both lines until it finds the 3 letter abbreviation for the site in both files. For example one file only contains BEC (sites2.txt - the city) and the other file (R03Sites2.csv - Region) contains 50 lines with a different cities and all the pertinent information.

    Sub Main()
    '--------------------------------------------------------------------------------------------------------
    'Declare Variables
    Dim strSiteCode
    Dim strVistaFQDN
    Dim strPort
    Dim arrStr

        '--------------------------------------------------------------------------------------------------------
        'Start processing
    
        Using sr As New StreamReader("c:\\dell\\R03Sites2.csv")
            Dim line As String
            ' Read and display lines from the file until the end of
            ' the file is reached.
            Do
                line = sr.ReadLine()
                Dim Sites As String = My.Computer.FileSystem.ReadAllText("c:\\dell\\Sites2.txt")
                If Not (line Is Nothing) Then
                    Console.WriteLine("Start processing loop")
                    arrStr = Split(sr.ReadLine, ",")
                    strSiteCode = arrStr(0)
                    strVistaFQDN = arrStr(1)
                    strPort = arrStr(2)
                    Console.WriteLine("Array populated...  Enter if then statement")
                    If Trim(Sites) = Trim(strSiteCode) Then
                        Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
                        Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
    
                        'build shorcuts here.  Code to build shortcuts is working correctly when ran
                        'statically but it never finds a match so it always moves to the else and writes
                        'Console.WriteLine("Error:  No Matching Information in the Sites and R03sites files!")
                        'There is only 1 line is Sites and an array is created for the data in R03sites.
                        'My intention is to match the line in Sites to the portion of the a
    
    Richard DeemingR 1 Reply Last reply
    0
    • U User 11833437

      I am attempting to build a command line tool that will create shortcuts for an application on the start menu and desktop. I have existing code that I use elsewhere to create the shortcuts. However, the program looks at two different files one file has only the site code and the other has the other information needed to created the shortcut by site for a whole region. The idea is to go through both lines until it finds the 3 letter abbreviation for the site in both files. For example one file only contains BEC (sites2.txt - the city) and the other file (R03Sites2.csv - Region) contains 50 lines with a different cities and all the pertinent information.

      Sub Main()
      '--------------------------------------------------------------------------------------------------------
      'Declare Variables
      Dim strSiteCode
      Dim strVistaFQDN
      Dim strPort
      Dim arrStr

          '--------------------------------------------------------------------------------------------------------
          'Start processing
      
          Using sr As New StreamReader("c:\\dell\\R03Sites2.csv")
              Dim line As String
              ' Read and display lines from the file until the end of
              ' the file is reached.
              Do
                  line = sr.ReadLine()
                  Dim Sites As String = My.Computer.FileSystem.ReadAllText("c:\\dell\\Sites2.txt")
                  If Not (line Is Nothing) Then
                      Console.WriteLine("Start processing loop")
                      arrStr = Split(sr.ReadLine, ",")
                      strSiteCode = arrStr(0)
                      strVistaFQDN = arrStr(1)
                      strPort = arrStr(2)
                      Console.WriteLine("Array populated...  Enter if then statement")
                      If Trim(Sites) = Trim(strSiteCode) Then
                          Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
                          Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
      
                          'build shorcuts here.  Code to build shortcuts is working correctly when ran
                          'statically but it never finds a match so it always moves to the else and writes
                          'Console.WriteLine("Error:  No Matching Information in the Sites and R03sites files!")
                          'There is only 1 line is Sites and an array is created for the data in R03sites.
                          'My intention is to match the line in Sites to the portion of the a
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You're getting the exception because you've read a line from the file which doesn't contain two commas. That's probably caused by the fact that you're skipping every other line:

      line = sr.ReadLine() ' Read a line
      If Not (line Is Nothing) Then
      arrStr = Split(sr.ReadLine, ",") ' Read another line

      You need to change that to re-use the line you've already read. You also need to check that the array returned from Split contains enough elements:

      line = sr.ReadLine()
      If line IsNot Nothing Then
      arrStr = Split(line, ",")
      If arrStr.Length >= 3 Then
      strSiteCode = arrStr(0)
      strVistaFQDN = arrStr(1)
      strPort = arrStr(2)
      ...

      You should also move the code that reads the contents of Sites2.txt outside of the loop. As it stands, you're reading the entire file for every line in Sites2.csv, even though you're not changing it.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      U 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        You're getting the exception because you've read a line from the file which doesn't contain two commas. That's probably caused by the fact that you're skipping every other line:

        line = sr.ReadLine() ' Read a line
        If Not (line Is Nothing) Then
        arrStr = Split(sr.ReadLine, ",") ' Read another line

        You need to change that to re-use the line you've already read. You also need to check that the array returned from Split contains enough elements:

        line = sr.ReadLine()
        If line IsNot Nothing Then
        arrStr = Split(line, ",")
        If arrStr.Length >= 3 Then
        strSiteCode = arrStr(0)
        strVistaFQDN = arrStr(1)
        strPort = arrStr(2)
        ...

        You should also move the code that reads the contents of Sites2.txt outside of the loop. As it stands, you're reading the entire file for every line in Sites2.csv, even though you're not changing it.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        U Offline
        U Offline
        User 11833437
        wrote on last edited by
        #3

        Thanks for the response. I made the suggested changes and am still having issues. This is what I have now:

        Sub Main()
        '--------------------------------------------------------------------------------------------------------
        'Declare Variables
        Dim strSiteCode
        Dim strVistaFQDN
        Dim strPort
        Dim arrStr
        Dim Sites As String = My.Computer.FileSystem.ReadAllText("c:\dell\Sites2.txt")
        '--------------------------------------------------------------------------------------------------------
        'Start processing

            Using sr As New StreamReader("c:\\dell\\R03Sites3.csv")
                Dim line As String
                ' Read and display lines from the file until the end of
                ' the file is reached.
        
                Do
                    line = sr.ReadLine()
                    Console.WriteLine("line = " & line)
                    Console.WriteLine("Start processing loop")
                    If line IsNot Nothing Then
                        arrStr = Split(line, ",")
                        If arrStr.Length >= 3 Then
                            strSiteCode = arrStr(0)
                            strVistaFQDN = arrStr(1)
                            strPort = arrStr(2)
                            Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
                            Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
                            Console.WriteLine("Array populated...  Enter if then statement")
                            If Trim(Sites) = Trim(strSiteCode) Then
                                'Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
                                'Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
                                Console.WriteLine("Successfully processed!")
                                Exit Do
                            Else
                                Console.WriteLine("Error:  No Matching Information in the Sitecode and R03sites files!")
                            End If
                        End If
                    End If
                Loop Until line Is Nothing
            End Using
        End Sub
        

        Here is the output (I cut the R03 file down to 2 entries): C:\Dell>bcmass line = BEC,test.beckley.com,19233 Start processing loop BEC,BEC , BEC s=test.beckley.com p=19233 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = BIR,test.birmingham.com,19230 Start processing loop BEC,BEC ,

        L 1 Reply Last reply
        0
        • U User 11833437

          Thanks for the response. I made the suggested changes and am still having issues. This is what I have now:

          Sub Main()
          '--------------------------------------------------------------------------------------------------------
          'Declare Variables
          Dim strSiteCode
          Dim strVistaFQDN
          Dim strPort
          Dim arrStr
          Dim Sites As String = My.Computer.FileSystem.ReadAllText("c:\dell\Sites2.txt")
          '--------------------------------------------------------------------------------------------------------
          'Start processing

              Using sr As New StreamReader("c:\\dell\\R03Sites3.csv")
                  Dim line As String
                  ' Read and display lines from the file until the end of
                  ' the file is reached.
          
                  Do
                      line = sr.ReadLine()
                      Console.WriteLine("line = " & line)
                      Console.WriteLine("Start processing loop")
                      If line IsNot Nothing Then
                          arrStr = Split(line, ",")
                          If arrStr.Length >= 3 Then
                              strSiteCode = arrStr(0)
                              strVistaFQDN = arrStr(1)
                              strPort = arrStr(2)
                              Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
                              Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
                              Console.WriteLine("Array populated...  Enter if then statement")
                              If Trim(Sites) = Trim(strSiteCode) Then
                                  'Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
                                  'Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
                                  Console.WriteLine("Successfully processed!")
                                  Exit Do
                              Else
                                  Console.WriteLine("Error:  No Matching Information in the Sitecode and R03sites files!")
                              End If
                          End If
                      End If
                  Loop Until line Is Nothing
              End Using
          End Sub
          

          Here is the output (I cut the R03 file down to 2 entries): C:\Dell>bcmass line = BEC,test.beckley.com,19233 Start processing loop BEC,BEC , BEC s=test.beckley.com p=19233 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = BIR,test.birmingham.com,19230 Start processing loop BEC,BEC ,

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The reason you are getting the extra data in the output is because of the line:

          Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))

          You need to split the Sites data the same as the other lines, so you can compare BEC to BEC. Also, you only read the Sites data once, so how do you expect it to be different on subsequent passes through the loop?

          U 2 Replies Last reply
          0
          • L Lost User

            The reason you are getting the extra data in the output is because of the line:

            Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))

            You need to split the Sites data the same as the other lines, so you can compare BEC to BEC. Also, you only read the Sites data once, so how do you expect it to be different on subsequent passes through the loop?

            U Offline
            U Offline
            User 11833437
            wrote on last edited by
            #5

            Thanks for the response. You're suggestion helped but the if then statement still doesn't work. Also, Sites data never changes each computer at each site has a Sites data file with the appropriate entry. It only has one line in the file and it is formatted like this: BEC,BEC. Here is the new code:

            Do
            line = sr.ReadLine()
            Console.WriteLine("line = " & line)
            Console.WriteLine("Start processing loop")
            If line IsNot Nothing Then
            arrStr = Split(line, ",")
            siteArr = Split(Sites, ",")
            Site1 = siteArr(0)
            If arrStr.Length >= 3 Then
            strSiteCode = arrStr(0)
            strVistaFQDN = arrStr(1)
            strPort = arrStr(2)
            Console.WriteLine(Trim(Site1) & " , " & Trim(strSiteCode))
            Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
            Console.WriteLine("Array populated... Enter if then statement")
            If Trim(Sites) = Trim(strSiteCode) Then
            'Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
            'Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
            Console.WriteLine("Successfully processed!")
            Exit Do
            Else
            Console.WriteLine("Error: No Matching Information in the Sitecode and R03sites files!")
            End If
            End If
            End If
            Loop Until line Is Nothing

            Here is the output: \Dell>bcmass line = BEC,test.beckley.com,19233 Start processing loop BEC , BEC s=test.beckley.com p=19233 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = BIR,test.birmingham.com,19230 Start processing loop BEC , BIR s=test.birmingham.com p=19230 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = Start processing loop

            L 1 Reply Last reply
            0
            • L Lost User

              The reason you are getting the extra data in the output is because of the line:

              Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))

              You need to split the Sites data the same as the other lines, so you can compare BEC to BEC. Also, you only read the Sites data once, so how do you expect it to be different on subsequent passes through the loop?

              U Offline
              U Offline
              User 11833437
              wrote on last edited by
              #6

              This line If Trim(Sites) = Trim(strSiteCode) Then is supposed to find the matching entry in both Sites and R03sites files ie. BEC , BEC and then process the shortcut creation and write to console if no match is found. Then loop through the rest of the file. I would like to learn how to make it stop when it finds the match but I can't even make it work the way I need it to yet.

              L 1 Reply Last reply
              0
              • U User 11833437

                Thanks for the response. You're suggestion helped but the if then statement still doesn't work. Also, Sites data never changes each computer at each site has a Sites data file with the appropriate entry. It only has one line in the file and it is formatted like this: BEC,BEC. Here is the new code:

                Do
                line = sr.ReadLine()
                Console.WriteLine("line = " & line)
                Console.WriteLine("Start processing loop")
                If line IsNot Nothing Then
                arrStr = Split(line, ",")
                siteArr = Split(Sites, ",")
                Site1 = siteArr(0)
                If arrStr.Length >= 3 Then
                strSiteCode = arrStr(0)
                strVistaFQDN = arrStr(1)
                strPort = arrStr(2)
                Console.WriteLine(Trim(Site1) & " , " & Trim(strSiteCode))
                Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
                Console.WriteLine("Array populated... Enter if then statement")
                If Trim(Sites) = Trim(strSiteCode) Then
                'Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
                'Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
                Console.WriteLine("Successfully processed!")
                Exit Do
                Else
                Console.WriteLine("Error: No Matching Information in the Sitecode and R03sites files!")
                End If
                End If
                End If
                Loop Until line Is Nothing

                Here is the output: \Dell>bcmass line = BEC,test.beckley.com,19233 Start processing loop BEC , BEC s=test.beckley.com p=19233 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = BIR,test.birmingham.com,19230 Start processing loop BEC , BIR s=test.birmingham.com p=19230 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = Start processing loop

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                If Trim(Sites) = Trim(strSiteCode) Then

                That should be

                If Trim(Site1) = Trim(strSiteCode) Then ' Site1 contains the first field of the Sites data

                You need to slow down, think what you are trying to achieve, and look at exactly what each line is doing. Rather than using Console.WriteLine to see what is going on, you should use the debugger step by step, so you can see exactly what is happening. And in the second loop (as I explained previously) BEC will never be equal to BIR.

                U 2 Replies Last reply
                0
                • L Lost User

                  If Trim(Sites) = Trim(strSiteCode) Then

                  That should be

                  If Trim(Site1) = Trim(strSiteCode) Then ' Site1 contains the first field of the Sites data

                  You need to slow down, think what you are trying to achieve, and look at exactly what each line is doing. Rather than using Console.WriteLine to see what is going on, you should use the debugger step by step, so you can see exactly what is happening. And in the second loop (as I explained previously) BEC will never be equal to BIR.

                  U Offline
                  U Offline
                  User 11833437
                  wrote on last edited by
                  #8

                  That totally makes sense. I just didn't change sites to site1 in all instances. I know that BEC will never equal BIR, R03sites has numerous sites and what I wanted to do was loop through all the sites in R03sites and write an error to the console on non-matching items and process the match. I just don't know how to do what I want a better way. I am sure you could make the loop stop when it finds a match but I don't know how to do it yet. I am still new to programming and have never worked with 2 text files at once, I have mostly just used text files to read computer names or ip addresses and then do something straightforward with it or write text to a log file. I appreciate the help. I left work but will give this a try tomorrow.

                  1 Reply Last reply
                  0
                  • L Lost User

                    If Trim(Sites) = Trim(strSiteCode) Then

                    That should be

                    If Trim(Site1) = Trim(strSiteCode) Then ' Site1 contains the first field of the Sites data

                    You need to slow down, think what you are trying to achieve, and look at exactly what each line is doing. Rather than using Console.WriteLine to see what is going on, you should use the debugger step by step, so you can see exactly what is happening. And in the second loop (as I explained previously) BEC will never be equal to BIR.

                    U Offline
                    U Offline
                    User 11833437
                    wrote on last edited by
                    #9

                    Thanks for the help. It worked perfectly.

                    L 1 Reply Last reply
                    0
                    • U User 11833437

                      Thanks for the help. It worked perfectly.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      You're welcome.

                      1 Reply Last reply
                      0
                      • U User 11833437

                        This line If Trim(Sites) = Trim(strSiteCode) Then is supposed to find the matching entry in both Sites and R03sites files ie. BEC , BEC and then process the shortcut creation and write to console if no match is found. Then loop through the rest of the file. I would like to learn how to make it stop when it finds the match but I can't even make it work the way I need it to yet.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Member 11866893 wrote:

                        Then loop through the rest of the file. I would like to learn how to make it stop

                        break;

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups