Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to increment an array within a loop?

Status
Not open for further replies.

pandu101

MIS
Sep 20, 2010
17
US
I am using the code below to parse thru several XML files and print out data from each XML file. It only looks for files that starts with BEX and that are older than 2 days. However the code does not run if there are more than one file to parse. At that point I get the error -- VBSCript runtime error: Object required:'ElemList.item(...)'

But it runs fine if there is only one file to parse. I think the error is caused by the array not incrementing (see "server = ElemList.item(0).Text" in the code below ).

But I am not really sure - would appreciate any advice.

TIA

Code:
Dim server, filepath 
Dim fso,fold,fil,XMLDoc 

Set fso = CreateObject("Scripting.FileSystemObject") 
Set fold = fso.GetFolder("C:\Program Files\Symantec\Backup Exec\Data") 

For each fil in fold.files 
	if (DateDiff("d",Now,fil.DateCreated) < 0) And (UCase(Left((fil.Name),3)) = "BEX") Then
	         Set xmlDoc = CreateObject("Msxml2.DOMDocument") 

	         fil = fold & "\" & fil.name 
			 wscript.echo fil
	         xmlDoc.load(fil) 
     
	     	 Set ElemList = xmlDoc.getElementsByTagName("jobServer") 
     		 server = ElemList.item(0).Text 
     		 Wscript.Echo server 

  
	         Set ElemList = xmlDoc.getElementsByTagName("timeStart") 
	         start_time = Replace(ElemList.item(0).Text,"Job started:","") 
        	 Wscript.Echo start_time 
          
          
        	 Set ElemList = xmlDoc.getElementsByTagName("timeEnd") 
       		 end_time = Replace(ElemList.item(0).Text,"Job ended:","") 
       		 Wscript.Echo end_time 
          
	         Set ElemList = xmlDoc.getElementsByTagName("completeStatus") 
	         engine_completion_status = Replace(ElemList.item(0).Text,"Job completion status:","") 
	         Wscript.Echo engine_completion_status 

	End if
Next
 
[0] The script at the present state has so much loose ends, it effectively renders it of practically zero scope of usefulness!

[1]> I think the error is caused by the array not incrementing
Not really, it is caused by the file either not really an xml file or being one but without the targetted item.

[2] Here is an extensive revision to salvage it, whether it performs 100% as needed I cannot assure, but I think it at least becomes more reasonable. It is up to you to understand why the changes.
[tt]
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder("C:\Program Files\Symantec\Backup Exec\Data")

[blue]Set xmlDoc = CreateObject("Msxml2.DOMDocument") [/blue]
For each fil in fold.files
if (DateDiff("d",Now,fil.DateCreated) < [red]-1[/red]) And (UCase(Left((fil.Name),3)) = "BEX") Then
fil[blue]path[/blue] = [blue]fil.path[/blue] 'better of a different variable name or spare it changing the following lines
wscript.echo fil[blue]path[/blue]
[blue]bret=[/blue]xmlDoc.load(fil[blue]path[/blue])
[blue]if bret then[/blue]
Set ElemList = xmlDoc.getElementsByTagName("jobServer")
[blue]if not ElemList is nothing then[/blue]
server = ElemList.item(0).Text
Wscript.Echo server
[blue]end if[/blue]

Set ElemList = xmlDoc.getElementsByTagName("timeStart")
[blue]if ElemList.length<>0 then[/blue]
start_time = Replace(ElemList.item(0).Text,"Job started:","")
Wscript.Echo start_time
[blue]end if[/blue]

Set ElemList = xmlDoc.getElementsByTagName("timeEnd")
[blue]if ElemList.length<>0 then[/blue]
end_time = Replace(ElemList.item(0).Text,"Job ended:","")
Wscript.Echo end_time
[blue]end if[/blue]

Set ElemList = xmlDoc.getElementsByTagName("completeStatus")
[blue]if ElemList.length<>0 then[/blue]
engine_completion_status = Replace(ElemList.item(0).Text,"Job completion status:","")
Wscript.Echo engine_completion_status
[blue]end if[/blue]
[blue]end if[/blue]
End if
Next
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top