Solved

Script to check whether text has been added to text file

Posted on 2011-02-11
1
307 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

28 Experts available now in Live!

Get 1:1 Help Now