title: Find specific string in a text file with VBS script
body: <p>I need to find the string "Test Case " &amp;  index in a txt file.</p>
  
  <p>I give you an example of the lines you can find in this file:</p>
  
  <pre><code>&lt;tr&gt;&lt;td&gt;&lt;a href="../../Login/Log_ in_U1A1"&gt;Log_ in_U1A1&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&lt;a href="../Test case 5 DD/Form1"&gt;Form1&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>As you can see in the second line I have an occurrence of the string "Test Case". </p>
  
  <p>What I want to do is to add another particular string in the line which preceeds the one where "Test Case 5" appears. For example:</p>
  
  <pre><code>&lt;tr&gt;&lt;td&gt;&lt;a href="../../Login/Log_ in_U1A1"&gt;Log_ in_U1A1&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&lt;a href="../../Logs/DD/Beginning_of_DD_TC5.html"&gt;Beginning_of_DD_TC5&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&lt;a href="../Test case 5 DD/Form1"&gt;Form1&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>It's also important that the line I add has an index i which depends on the Test Case number, and i need to add it before the first occurrence of "Test Case" &amp; i, i dont care about the following occurrences.</p>
  
  <p>I tested if InStr function worked with an example:</p>
  
  <pre><code>Dim objFSO, filepath, objInputFile, tmpStr, substrToFind
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  filepath = "C:\VBS\filediprova.txt"
  substrToFind = "&lt;tr&gt;&lt;td&gt;&lt;a href=" &amp; chr(34) &amp; "../Test case 5"
  Set objInputFile = objFSO.OpenTextFile(filepath)
  tmpStr = objInputFile.ReadLine
  If InStr(tmpStr, substrToFind) &lt;= 0 Then
     WScript.Echo "No matches"
  Else
     WScript.Echo "Found match"
  End If
  </code></pre>
  
  <p>And it works, it recognizes my substring. In this small example the txt file only contans the followingline:</p>
  
  <pre><code>&lt;tr&gt;&lt;td&gt;&lt;a href="../Test case 5 DD/Form1"&gt;Form1&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>Now, when I try to loop over a file with much more lines I have some problem, I use the same InStr function.
  I wrote the following loop:</p>
  
  <pre><code>Do until objInputFile.AtEndOfStream
     strToAdd = "&lt;tr&gt;&lt;td&gt;&lt;a href=" &amp; chr(34) &amp; "../../Logs/DD/Beginning_of_DD_TC" &amp; CStr(index) &amp; ".html" &amp; chr(34) &amp; "&gt;Beginning_of_DD_TC" &amp; CStr(index) &amp; "&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;"
     substrToFind = "&lt;tr&gt;&lt;td&gt;&lt;a href=" &amp; chr(34) &amp; "../Test case " &amp; index
     firstStr = "&lt;?xml version" 'my file always starts like this
     tmpStr = objInputFile.ReadLine
     If InStr(tmpStr, substrToFind) &lt;= 0 Then
         If Instr(tmpStr, firstStr) &gt; 0 Then
            text = tmpStr 'to avoid the first empty line
         Else
            text = text &amp; vbCrLf &amp; tmpStr
         End If
     Else
        text = text &amp; vbCrLf &amp; strToAdd &amp; vbCrLf &amp; tmpStr
        index = index + 1
     End If
  Loop
  </code></pre>
  
  <p>What's wrong?</p>