Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find specific string in a text file with VBS script

I need to find the string "Test Case " & index in a txt file.

I give you an example of the lines you can find in this file:

<tr><td><a href="../../Login/Log_ in_U1A1">Log_ in_U1A1</a></td></tr>
<tr><td><a href="../Test case 5 DD/Form1">Form1</a></td></tr>

As you can see in the second line I have an occurrence of the string "Test Case".

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:

<tr><td><a href="../../Login/Log_ in_U1A1">Log_ in_U1A1</a></td></tr>
<tr><td><a href="../../Logs/DD/Beginning_of_DD_TC5.html">Beginning_of_DD_TC5</a></td></tr>
<tr><td><a href="../Test case 5 DD/Form1">Form1</a></td></tr>

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" & i, i dont care about the following occurrences.

I tested if InStr function worked with an example:

Dim objFSO, filepath, objInputFile, tmpStr, substrToFind
Set objFSO = CreateObject("Scripting.FileSystemObject")
filepath = "C:\VBS\filediprova.txt"
substrToFind = "<tr><td><a href=" & chr(34) & "../Test case 5"
Set objInputFile = objFSO.OpenTextFile(filepath)
tmpStr = objInputFile.ReadLine
If InStr(tmpStr, substrToFind) <= 0 Then
   WScript.Echo "No matches"
Else
   WScript.Echo "Found match"
End If

And it works, it recognizes my substring. In this small example the txt file only contans the followingline:

<tr><td><a href="../Test case 5 DD/Form1">Form1</a></td></tr>

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:

Do until objInputFile.AtEndOfStream
   strToAdd = "<tr><td><a href=" & chr(34) & "../../Logs/DD/Beginning_of_DD_TC" & CStr(index) & ".html" & chr(34) & ">Beginning_of_DD_TC" & CStr(index) & "</a></td></tr>"
   substrToFind = "<tr><td><a href=" & chr(34) & "../Test case " & index
   firstStr = "<?xml version" 'my file always starts like this
   tmpStr = objInputFile.ReadLine
   If InStr(tmpStr, substrToFind) <= 0 Then
       If Instr(tmpStr, firstStr) > 0 Then
          text = tmpStr 'to avoid the first empty line
       Else
          text = text & vbCrLf & tmpStr
       End If
   Else
      text = text & vbCrLf & strToAdd & vbCrLf & tmpStr
      index = index + 1
   End If
Loop

What's wrong?

like image 842
Luceye85 Avatar asked May 25 '13 15:05

Luceye85


People also ask

How do I find a string in VBScript?

The InStr function returns the position of the first occurrence of one string within another. The InStr function can return the following values: If string1 is "" - InStr returns 0. If string1 is Null - InStr returns Null.

What is string in VBScript?

VB.NET Masterclass: Learn Visual Basic and VBScript Strings are a sequence of characters, which can consist of alphabets or numbers or special characters or all of them. A variable is said to be a string if it is enclosed within double quotes " ".


1 Answers

I'd recommend using a regular expressions instead of string operations for this:

Set fso = CreateObject("Scripting.FileSystemObject")

filename = "C:\VBS\filediprova.txt"

newtext = vbLf & "<tr><td><a href=""..."">Beginning_of_DD_TC5</a></td></tr>"

Set re = New RegExp
re.Pattern = "(\n.*?Test Case \d)"
re.Global  = False
re.IgnoreCase = True

text = f.OpenTextFile(filename).ReadAll
f.OpenTextFile(filename, 2).Write re.Replace(text, newText & "$1")

The regular expression will match a line feed (\n) followed by a line containing the string Test Case followed by a number (\d), and the replacement will prepend that with the text you want to insert (variable newtext). Setting re.Global = False makes the replacement stop after the first match.

If the line breaks in your text file are encoded as CR-LF (carriage return + line feed) you'll have to change \n into \r\n and vbLf into vbCrLf.

If you have to modify several text files, you could do it in a loop like this:

For Each f In fso.GetFolder("C:\VBS").Files
  If LCase(fso.GetExtensionName(f.Name)) = "txt" Then
    text = f.OpenAsTextStream.ReadAll
    f.OpenAsTextStream(2).Write re.Replace(text, newText & "$1")
  End If
Next
like image 143
Ansgar Wiechers Avatar answered Sep 28 '22 03:09

Ansgar Wiechers