Looping through files to compare

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Justin Fancy

    #1

    Looping through files to compare

    Hi Everyone,

    I'm lookin for a very confusing loop (to me), to compare two files.
    Here it is. I have two arrays with paths stored in both. example:
    /en/aviation/you.htm.

    I need to search array two for all the paths in array 1. If it finds
    it, send it to array 3.

    Here's my code to send it to the array.

    Dim sr As IO.StreamReader = IO.File.OpenTex t(File1)

    'Loop through Directory Listing file to load filepaths into an
    array
    While sr.Peek <-1

    'Count the number of lines (for the arrays), and read in
    the lines
    counter += 1
    lineFile1 = sr.ReadLine

    'Re-Dimension the array to hold "counter" number of
    elements
    ReDim InternetDirecto ryArray(counter )

    'String Manipulation (Replace some unneeded text)
    MyString = lineFile1

    Dim sb As New System.Text.Str ingBuilder(MySt ring)
    sb.Replace("C:" , "")
    sb.Replace("x:" , "")
    sb.Replace("\", "/")
    sb.Replace("www root", "")
    MyString = sb.ToString()

    InternetDirecto ryArray(counter ) = MyString

    End While
    'Close the first file
    sr.Close()


    'Open the second file
    sr = IO.File.OpenTex t(File2)
    i = 0
    i2 = 0
    counter2 = 0

    Do While sr.Peek <-1

    counter2 += 1
    lineFile2 = sr.ReadLine
    MyString2 = lineFile2
    Dim sb2 As New System.Text.Str ingBuilder(MySt ring2)
    sb2.Replace("C: ", "")
    sb2.Replace("x: ", "")
    sb2.Replace("\" , "/")
    sb2.Replace("ww wroot", "")
    MyString2 = sb2.ToString()
    ReDim Preserve IISLogArray(cou nter2)
    IISLogArray(cou nter2) = MyString2

    Loop

    --------------------------------------------------------------------

    I've tried for hours on how to get the logic down, but its confusing me
    too much. Maybe someone know's what to do.

    Thanks for your help.

    Justin

  • Justin Fancy

    #2
    Re: Looping through files to compare

    One more thing, Here's the code i've tried to tamper with, and it looks
    right? Just not executing the way I want it to?

    For i = 0 To counter
    If MyString2 <InternetDirect oryArray(i) Then
    MsgBox("This is not working")
    End If
    Next
    'lstDisplay.Ite ms.Add(ZeroHits (i2))

    This code is inside the second Do While loop, comparing it to the 1st
    array. I've tested it by putting msgbox's in between the if statement,
    but it doesn't work. How do i know that? Well, I put two of the same
    text files i'm reading in the same. Each line in each file are
    identical. So, if the code was executing the way I wanted it to, it
    wouldn't display the message box. But it does, repeatidly.

    Justin

    Comment

    • Steve Long

      #3
      Re: Looping through files to compare

      Justin,
      basically what I'm hearing is that you're going to have to do the following:
      Load array1 with paths from a file
      Load array2 with paths from a file
      for every item in array1, loop through array2 to see if there is a match.

      That sounds really slow to me. I think I'd load the items into a dictionary
      or collection of some kind to make the searchs faster.
      Load file1 into a collection
      load file2 into a collection

      for every item in collection1,
      if collection2.ite m(item) then
      do something with it
      end if
      next

      Just a thought
      Steve


      "Justin Fancy" <justinfancy@gm ail.comwrote in message
      news:1161713124 .390584.6690@b2 8g2000cwb.googl egroups.com...
      Hi Everyone,
      >
      I'm lookin for a very confusing loop (to me), to compare two files.
      Here it is. I have two arrays with paths stored in both. example:
      /en/aviation/you.htm.
      >
      I need to search array two for all the paths in array 1. If it finds
      it, send it to array 3.
      >
      Here's my code to send it to the array.
      >
      Dim sr As IO.StreamReader = IO.File.OpenTex t(File1)
      >
      'Loop through Directory Listing file to load filepaths into an
      array
      While sr.Peek <-1
      >
      'Count the number of lines (for the arrays), and read in
      the lines
      counter += 1
      lineFile1 = sr.ReadLine
      >
      'Re-Dimension the array to hold "counter" number of
      elements
      ReDim InternetDirecto ryArray(counter )
      >
      'String Manipulation (Replace some unneeded text)
      MyString = lineFile1
      >
      Dim sb As New System.Text.Str ingBuilder(MySt ring)
      sb.Replace("C:" , "")
      sb.Replace("x:" , "")
      sb.Replace("\", "/")
      sb.Replace("www root", "")
      MyString = sb.ToString()
      >
      InternetDirecto ryArray(counter ) = MyString
      >
      End While
      'Close the first file
      sr.Close()
      >
      >
      'Open the second file
      sr = IO.File.OpenTex t(File2)
      i = 0
      i2 = 0
      counter2 = 0
      >
      Do While sr.Peek <-1
      >
      counter2 += 1
      lineFile2 = sr.ReadLine
      MyString2 = lineFile2
      Dim sb2 As New System.Text.Str ingBuilder(MySt ring2)
      sb2.Replace("C: ", "")
      sb2.Replace("x: ", "")
      sb2.Replace("\" , "/")
      sb2.Replace("ww wroot", "")
      MyString2 = sb2.ToString()
      ReDim Preserve IISLogArray(cou nter2)
      IISLogArray(cou nter2) = MyString2
      >
      Loop
      >
      --------------------------------------------------------------------
      >
      I've tried for hours on how to get the logic down, but its confusing me
      too much. Maybe someone know's what to do.
      >
      Thanks for your help.
      >
      Justin
      >

      Comment

      • Steve Long

        #4
        Re: Looping through files to compare

        Opps, this code:
        if collection2.ite m(item) then

        should have read like this:

        if not collection2.ite m(item) is nothing then
        'do something
        end if

        S

        "Steve Long" <Steve_Noneya@N oSpam.comwrote in message
        news:etN0e559GH A.3344@TK2MSFTN GP03.phx.gbl...
        Justin,
        basically what I'm hearing is that you're going to have to do the
        following:
        Load array1 with paths from a file
        Load array2 with paths from a file
        for every item in array1, loop through array2 to see if there is a match.
        >
        That sounds really slow to me. I think I'd load the items into a
        dictionary or collection of some kind to make the searchs faster.
        Load file1 into a collection
        load file2 into a collection
        >
        for every item in collection1,
        if collection2.ite m(item) then
        do something with it
        end if
        next
        >
        Just a thought
        Steve
        >
        >
        "Justin Fancy" <justinfancy@gm ail.comwrote in message
        news:1161713124 .390584.6690@b2 8g2000cwb.googl egroups.com...
        >Hi Everyone,
        >>
        >I'm lookin for a very confusing loop (to me), to compare two files.
        >Here it is. I have two arrays with paths stored in both. example:
        >/en/aviation/you.htm.
        >>
        >I need to search array two for all the paths in array 1. If it finds
        >it, send it to array 3.
        >>
        >Here's my code to send it to the array.
        >>
        >Dim sr As IO.StreamReader = IO.File.OpenTex t(File1)
        >>
        > 'Loop through Directory Listing file to load filepaths into an
        >array
        > While sr.Peek <-1
        >>
        > 'Count the number of lines (for the arrays), and read in
        >the lines
        > counter += 1
        > lineFile1 = sr.ReadLine
        >>
        > 'Re-Dimension the array to hold "counter" number of
        >elements
        > ReDim InternetDirecto ryArray(counter )
        >>
        > 'String Manipulation (Replace some unneeded text)
        > MyString = lineFile1
        >>
        > Dim sb As New System.Text.Str ingBuilder(MySt ring)
        > sb.Replace("C:" , "")
        > sb.Replace("x:" , "")
        > sb.Replace("\", "/")
        > sb.Replace("www root", "")
        > MyString = sb.ToString()
        >>
        > InternetDirecto ryArray(counter ) = MyString
        >>
        > End While
        > 'Close the first file
        > sr.Close()
        >>
        >>
        > 'Open the second file
        > sr = IO.File.OpenTex t(File2)
        > i = 0
        > i2 = 0
        > counter2 = 0
        >>
        > Do While sr.Peek <-1
        >>
        > counter2 += 1
        > lineFile2 = sr.ReadLine
        > MyString2 = lineFile2
        > Dim sb2 As New System.Text.Str ingBuilder(MySt ring2)
        > sb2.Replace("C: ", "")
        > sb2.Replace("x: ", "")
        > sb2.Replace("\" , "/")
        > sb2.Replace("ww wroot", "")
        > MyString2 = sb2.ToString()
        > ReDim Preserve IISLogArray(cou nter2)
        > IISLogArray(cou nter2) = MyString2
        >>
        > Loop
        >>
        >--------------------------------------------------------------------
        >>
        >I've tried for hours on how to get the logic down, but its confusing me
        >too much. Maybe someone know's what to do.
        >>
        >Thanks for your help.
        >>
        >Justin
        >>
        >
        >

        Comment

        • Justin Fancy

          #5
          Re: Looping through files to compare

          Sorry, I'm not up on using collections at all.

          I dont have the code with me now, but I did get the logic down, but I
          can't seem to get it working.

          I'll message tomorrow morning. Thanks for your help Steve. Much
          appreciated.

          J

          Steve Long wrote:
          Opps, this code:
          if collection2.ite m(item) then
          >
          should have read like this:
          >
          if not collection2.ite m(item) is nothing then
          'do something
          end if
          >
          S
          >
          "Steve Long" <Steve_Noneya@N oSpam.comwrote in message
          news:etN0e559GH A.3344@TK2MSFTN GP03.phx.gbl...
          Justin,
          basically what I'm hearing is that you're going to have to do the
          following:
          Load array1 with paths from a file
          Load array2 with paths from a file
          for every item in array1, loop through array2 to see if there is a match.

          That sounds really slow to me. I think I'd load the items into a
          dictionary or collection of some kind to make the searchs faster.
          Load file1 into a collection
          load file2 into a collection

          for every item in collection1,
          if collection2.ite m(item) then
          do something with it
          end if
          next

          Just a thought
          Steve


          "Justin Fancy" <justinfancy@gm ail.comwrote in message
          news:1161713124 .390584.6690@b2 8g2000cwb.googl egroups.com...
          Hi Everyone,
          >
          I'm lookin for a very confusing loop (to me), to compare two files.
          Here it is. I have two arrays with paths stored in both. example:
          /en/aviation/you.htm.
          >
          I need to search array two for all the paths in array 1. If it finds
          it, send it to array 3.
          >
          Here's my code to send it to the array.
          >
          Dim sr As IO.StreamReader = IO.File.OpenTex t(File1)
          >
          'Loop through Directory Listing file to load filepaths into an
          array
          While sr.Peek <-1
          >
          'Count the number of lines (for the arrays), and read in
          the lines
          counter += 1
          lineFile1 = sr.ReadLine
          >
          'Re-Dimension the array to hold "counter" number of
          elements
          ReDim InternetDirecto ryArray(counter )
          >
          'String Manipulation (Replace some unneeded text)
          MyString = lineFile1
          >
          Dim sb As New System.Text.Str ingBuilder(MySt ring)
          sb.Replace("C:" , "")
          sb.Replace("x:" , "")
          sb.Replace("\", "/")
          sb.Replace("www root", "")
          MyString = sb.ToString()
          >
          InternetDirecto ryArray(counter ) = MyString
          >
          End While
          'Close the first file
          sr.Close()
          >
          >
          'Open the second file
          sr = IO.File.OpenTex t(File2)
          i = 0
          i2 = 0
          counter2 = 0
          >
          Do While sr.Peek <-1
          >
          counter2 += 1
          lineFile2 = sr.ReadLine
          MyString2 = lineFile2
          Dim sb2 As New System.Text.Str ingBuilder(MySt ring2)
          sb2.Replace("C: ", "")
          sb2.Replace("x: ", "")
          sb2.Replace("\" , "/")
          sb2.Replace("ww wroot", "")
          MyString2 = sb2.ToString()
          ReDim Preserve IISLogArray(cou nter2)
          IISLogArray(cou nter2) = MyString2
          >
          Loop
          >
          --------------------------------------------------------------------
          >
          I've tried for hours on how to get the logic down, but its confusing me
          too much. Maybe someone know's what to do.
          >
          Thanks for your help.
          >
          Justin
          >

          Comment

          • Hugh Janus

            #6
            Re: Looping through files to compare


            Steve Long wrote:
            Justin,
            basically what I'm hearing is that you're going to have to do the following:
            Load array1 with paths from a file
            Load array2 with paths from a file
            for every item in array1, loop through array2 to see if there is a match.
            Load array1 into a hashtable. then load array2 into the same table.
            If you get an error 5 it means that the item already exists so you can
            add that item to array 3.

            Hugh

            Comment

            • Steve Long

              #7
              Re: Looping through files to compare

              Right Hugh, but, the problem with this method is that a exception is
              thrown--which is very expensive in terms of processor time. However, the
              other method of checking to see if an item exists in a collection should be
              faster. Maybe.... ????

              Steve

              "Hugh Janus" <my-junk-account@hotmail .comwrote in message
              news:1161759041 .156039.324760@ f16g2000cwb.goo glegroups.com.. .
              >
              Steve Long wrote:
              >Justin,
              >basically what I'm hearing is that you're going to have to do the
              >following:
              >Load array1 with paths from a file
              >Load array2 with paths from a file
              >for every item in array1, loop through array2 to see if there is a match.
              >
              Load array1 into a hashtable. then load array2 into the same table.
              If you get an error 5 it means that the item already exists so you can
              add that item to array 3.
              >
              Hugh
              >

              Comment

              Working...