?
Solved

VBscript loop error

Posted on 2011-05-06
5
Medium Priority
?
460 Views
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?

Thanks.

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
	Next

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

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

Open in new window

0
Comment
Question by:parktsinc
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
smstrickler earned 1200 total points
ID: 35707655
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.
0
 
LVL 59

Assisted Solution

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

CHRIS
0
 
LVL 59

Expert Comment

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

Chris
0
 

Author Comment

by:parktsinc
ID: 35708175
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.
0
 

Author Comment

by:parktsinc
ID: 35709122
Got it figured out.  I was using objFile as a variable for two different parts of the Do loop...Doh!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
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…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Screencast - Getting to Know the Pipeline

807 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