Solved

Script to check whether text has been added to text file

Posted on 2011-02-11
1
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

759 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