VBscript loop error

Posted on 2011-05-06
Last Modified: 2012-05-11
I pieced together some scripts to read through a list of computers and find a specific file.  The results are output to two separate text files for found and not found.  The script works the first time through, but when it loops I get an object required error.  Not sure what is causing the error.

Any ideas or corrections?


Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim strComputer()
Dim objFile
Dim isFound
Dim strComputersFile, strFound, strNotFound
Dim i

Dim objLogFile1, objLogFile2
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")

strComputersFile = "C:\lotus_notes\computers.txt"
strFound = "C:\lotus_notes\Found.txt"
strNotFound = "C:\lotus_notes\NotFound.txt"

Set objLogFile1 = objFSO.OpenTextFile(strNotFound, ForAppending, True)
Set objLogFile2 = objFSO.OpenTextFile(strFound, ForAppending, True)

Set objFile = objFSO.OpenTextFile(strComputersFile, ForReading)
'start parsing through computer.txt
i = 0
Do Until objFile.AtEndOfStream
isFound = False
Redim Preserve strComputer(i)
strComputer(i) = objFile.ReadLine

	WScript.Echo strComputer(i)

	Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & _
	strComputer(i) & "\root\cimv2")
	wscript.Echo "Querying " & strComputer(i)
	Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Drive='C:' AND (Extension='id')")
	For Each objFile in colFiles
	Wscript.Echo objFile.Name
	objLogFile2.WriteLine strComputer(i) & ";" & objFile.Name
	isFound = True

	If isFound = False Then
	objLogFile1.WriteLine Now & "No File matching was found on " & strComputer(i)
	End If
i = i + 1

WScript.Echo "Done scanning, LogFiles are located at:" & vbCrLf & strFound & vbCrLf & strNotFound & vbCrLf & "Click OK to finish"

Open in new window

Question by:parktsinc
    LVL 1

    Accepted Solution

    When you debug, on which line do you receive the error?  This type of error occurs when access to an object is lost, so I can see a couple of places where that could be happening.
    LVL 59

    Assisted Solution

    by:Chris Bottomley
    I would note that objfile is not closed T THE END OF YOUR SCRIPT SO THAT WOULD BE AN ISSUE THE SECOND TIME AROUND.

    LVL 59

    Expert Comment

    by:Chris Bottomley
    Oops, sorry about the caps switch mid way through!


    Author Comment

    I'm getting the error on line 23, Do Until, once it loops, it looks like the object is lost.

    As for closing the file, I'll be sure to add that, but it doesn't finish looping through the computers.txt file to get to a 2nd error.

    I only have 3 computers listed in computers.txt, the first one completes fine and then the object required error pops up.

    Author Comment

    Got it figured out.  I was using objFile as a variable for two different parts of the Do loop...Doh!

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    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…
    Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    745 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

    16 Experts available now in Live!

    Get 1:1 Help Now