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 compare file names (XML file names) 1

Status
Not open for further replies.

pandu101

MIS
Sep 20, 2010
17
0
0
US
Hi,

I have the following script that will go thru a folder containing several xml files and then extract data from those XML files. However, I want it to *only* parse XML files that begin with the letters BEX. How can I make this happen? Below is the script.

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) = 2 Then 
         Set xmlDoc = CreateObject("Msxml2.DOMDocument")  

         fil = fold & "\" & fil.name  
         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
 
Code:
.
.
.
For each fil in fold.files
if DateDiff("d",Now,fil.DateCreated) = 2 Then
[COLOR=red]if UCase(Left(fil.Name)) = "BEX" then[/color]
.
.
<do stuff>
.
.
.
[COLOR=red]end if[/color]
end if
next
 
Thanks jges - that worked! I combined the two if statements and now I get the error VBSCript runtime error: Object required:'ElemList.item(...)'
The error is in line 17 in the code. I think the error is caused because I am referring to the same item in the elemList array - maybe I need to advance the array pointer but not sure how to do this in vbscript. Or I could be totally off-base.

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
 
The first thing I would look at is the XML file. Does the "jobServer" tag exist in the file exactly as it appears in your script? In other words, are you sure it is "jobServer" and not "job server" or "JobServer" or some other variation?

Also, you will want to test that something is actually returned before you use the value. Something like:
Code:
if ElemList.Count > 0 then...
 
Also, for what its worth, you can shorten this:
Code:
fil = fold & "\" & fil.name
wscript.echo fil
xmlDoc.load(fil)

to this:
Code:
xmlDoc.load(objFSO.GetAbsolutePathName(fil))

 
Oops, in your case it would be:
Code:
xmlDoc.load(fso.GetAbsolutePathName(fil))
 
jges, you are right on! There was a mismatch. jobserver was actually listed only as server in the xml doc. While checking the server tag, I came across other issues though. I am looking for another tag called "start_time" and "end_time". But there are multiple "start_time" and "end_time" tags in each XML document. The XML doc is a log file from our backup software. I just want the first "start_time" and the last "end_time" within each document - this gives the actual start and end times of a backup job. Is there some way to get this info from the XML doc? Thanks.
 
If what you want is the first occurrence of "timeStart" and the last occurrence of "timeEnd" try this:
Code:
Set ElemList = xmlDoc.getElementsByTagName("timeStart")
start_time = ElemList.item(0).Text

Set ElemList = xmlDoc.getElementsByTagName("timeEnd")
end_time = ElemList.item(ElemList.Count).Text
 
Thanks again jges for a great suggestion. I excitedly tried it out but got an error (object does not support this property or method). Upon doing further research, it seems that vbscript does not have a function to return the array size - is this true or just old info? See -
PS - I also tried ubound - but no go
 
Sorry, change "ElemList.Count" to "ElemList.Length".
 
One more correction since the collection is 0 based: "ElemList.Length - 1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top