Solved

Script to check whether text has been added to text file

Posted on 2011-02-11
1
295 Views
Last Modified: 2012-05-11
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
Comment
Question by:itbabe
1 Comment
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34869766
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now