Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

Script to check whether text has been added to text file

I am using the following script to check whether a string being 'hello' was added to the following textfile.

But at line 16  being If LastCount = -1 Then , i get an error, see attachment


MyString = "hello"
MyTextFile = "C:\Documents and Settings\admhomaa\Desktop\test.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set myRegExp = new regexp  
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = MyString
LastCount = -1
NeverEnds = 0
Do Until NeverEnds = 1
        Set objFile = objFSO.OpenTextFile(MyTextFile, 1)
        X = objFile.Readall
        Set myMatches = myRegExp.Execute(X)
        If LastCount = -1 Then
                Set LastCount = myMatches
        Else
                If myMatches.count > LastCount Then
                MsgBox "Yes"
                LastCount = myMatches
                        WScript.Quit
                End If
        End If
        objFile.Close
        WScript.Sleep 5000 ' sleep for 5 seconds
Loop
 vbscript-error
0
itbabe
Asked:
itbabe
1 Solution
 
RobSampsonCommented:
The problem is that you're setting LastCount to a collection, being myMatches, and then comparing it to an integer number.  Set LastCount to myMatches.Count instead, and that should fix your problem.

Regards,

Rob.
MyString = "hello"
MyTextFile = "C:\Documents and Settings\admhomaa\Desktop\test.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set myRegExp = new regexp  
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = MyString
LastCount = -1
NeverEnds = 0
Do Until NeverEnds = 1
        Set objFile = objFSO.OpenTextFile(MyTextFile, 1)
        X = objFile.Readall
        Set myMatches = myRegExp.Execute(X)
        If LastCount = -1 Then
                LastCount = myMatches.Count
        Else
                If myMatches.count > LastCount Then
                MsgBox "Yes"
                LastCount = myMatches.Count
                        WScript.Quit
                End If
        End If
        objFile.Close
        WScript.Sleep 5000 ' sleep for 5 seconds
Loop

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now