Re: Loading mulitple files
From: Roland Hall (nobody_at_nowhere)
Date: 01/28/05
- Next message: Matthias Tacke: "Re: Collecting Windows NT 4.0 events"
- Previous message: mikn: "Re: Newbie search question"
- In reply to: Roland Hall: "Re: Loading mulitple files"
- Next in thread: Big D: "Re: Loading mulitple files"
- Reply: Big D: "Re: Loading mulitple files"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 28 Jan 2005 02:12:05 -0600
I've made a few cosmetic changes:
1. I removed logPath and included it as a Const for logFile.
2. I inserted some text nodes so you can view the log file with notepad and
it's not all on one line.
3. I now notify the admin with a result status and where the log file was
written to.
The only thing left, unless you wanted to pass the source/log path as
parameters is an XSLT stylesheet and code to render it.
I'll leave that as an exercise.
Anyone who can offer alternative routines in my code, especially for the
attributes, which was my first attempt, I'd appreciate the feedback.
This is a sample of the log file output.
<?xml version="1.0" encoding="UTF-8"?>
<logs>
<log date="1/28/2005 2:06:59 AM">
<application path="c:\b\application.xml" name="RCS" version="9.10.8"/>
<application path="c:\b\application.xml" name="Web" version="5.12.9"/>
<application path="c:\b\test1.xml" name="ADS Version" version="2.5"/>
<application path="c:\b\test1.xml" name="State City" version="3.8.0"/>
<application path="c:\b\test2.xml" name="Web" version="4.13.0"/>
</log>
<log date="1/28/2005 2:07:19 AM">
<application path="c:\b\application.xml" name="RCS" version="9.10.8"/>
<application path="c:\b\application.xml" name="Web" version="5.12.9"/>
<application path="c:\b\test1.xml" name="ADS Version" version="2.5"/>
<application path="c:\b\test1.xml" name="State City" version="3.8.0"/>
<application path="c:\b\test2.xml" name="Web" version="4.13.0"/>
</log>
<log date="1/28/2005 2:08:47 AM">
<application path="c:\b\application.xml" name="RCS" version="9.10.8"/>
<application path="c:\b\application.xml" name="Web" version="5.12.9"/>
<application path="c:\b\test1.xml" name="ADS Version" version="2.5"/>
<application path="c:\b\test1.xml" name="State City" version="3.8.0"/>
<application path="c:\b\test2.xml" name="Web" version="4.13.0"/>
</log>
</logs>
Here is the updated code:
Option Explicit
sub prt(str)
wscript.echo str
end sub
function fileList(folder)
dim file, files, ext
ext = ".xml"
for each file in folder.files
If lcase(right(file.name, 4)) = ext then
files = files & file.name & ","
end if
next
fileList = split(left(files,len(files)-1),",")
end function
sub log(strLog)
dim logXML, logtext, arrLogs, arrText
dim appNode, i, iMax, pi, root, node, att, text
set logXML = CreateObject("MSXML2.DOMDocument")
logXML.async = false
logXML.validateOnParse = false
logXML.resolveExternals = false
if fso.FileExists(logFile) Then
logXML.load logFile
else
logXML.loadXML("<logs/>")
set pi = logXML.createProcessingInstruction("xml", "version='1.0'
encoding='UTF-8'")
logXML.insertBefore pi, logXML.firstChild
set pi = nothing
end if
set root = logXML.documentElement
root.appendChild(logXML.createTextNode(vbCrLf & vbTab))
set node = logXML.createElement("log")
root.appendChild(node)
set att = logXML.createAttribute("date")
set text = logXML.createTextNode(now)
att.appendChild(text)
set node = logXML.documentElement.lastChild
node.setAttributeNode(att)
arrLogs = split(strLog,vbCrLf)
iMax = ubound(arrLogs)
if len(arrLogs(iMax)) = 0 then
iMax = iMax - 1
end if
for i = 0 to iMax
node.appendChild(logXML.createTextNode(vbLf & vbTab & vbTab))
set appNode = logXML.createElement("application")
node.appendChild(appNode)
arrText = split(arrLogs(i),",")
set att = logXML.createAttribute("path")
set text = logXML.createTextNode(arrText(0))
att.appendChild(text)
appNode.setAttributeNode(att)
set att = logXML.createAttribute("name")
set text = logXML.createTextNode(arrText(1))
att.appendChild(text)
appNode.setAttributeNode(att)
set att = logXML.createAttribute("version")
set text = logXML.createTextNode(arrText(2))
att.appendChild(text)
appNode.setAttributeNode(att)
next
node.appendChild(logXML.createTextNode(vbCrLf & vbTab))
root.appendChild(logXML.createTextNode(vbCrLf))
logXML.save logFile
set text = nothing
set att = nothing
set appNode = nothing
set node = nothing
set root = nothing
set logXML = nothing
end sub
function queryXML(xmlfiles)
dim sFilePath, sXPath, oXMLDoc, oErr, dApps, oApp, oApps, sApp, i, j
dim sErr, aTests, sTest, aVersions, sVersion, sItem
aTests = split("ADS Version,State City,RCS,Release,Web",",")
aVersions = split("2.7,3.9.0,9.11.0,3.5.009,5.13.0",",")
for i = 0 to ubound(xmlfiles)
sFIlePath = strPath & "\" & xmlfiles(i)
sXPath = "/Versioning/Application"
Set oXMLDoc = CreateObject("MSXML2.DOMDocument")
oXMLDoc.SetProperty "SelectionLanguage", "XPath"
oXMLDoc.Async = False
oXMLDoc.Load sFilePath
If (oXMLDoc.parseError.errorCode <> 0) Then
Set oErr = oXMLDoc.ParseError
WScript.Echo "Could not load file " & sFilePath _
& " , error: " & oErr.Reason
WScript.Quit
End If
Set dApps = CreateObject("Scripting.Dictionary")
dApps.CompareMode = vbTextCompare
Set oApps = oXMLDoc.DocumentElement.SelectNodes(sXPath)
For Each oApp in oApps
dApps.Add oApp.getAttribute("Name"), oApp.getAttribute("Version")
Next
for j = 0 to ubound(aTests)
sTest = aTests(j)
sVersion = aVersions(j)
sItem = dApps.Item(sTest)
if sItem <> sVersion Then
sErr = sErr & sFilePath & "," & sTest & "," & dApps.Item(sTest) &
vbCrLf
end if
next
next
queryXML = sErr
set oApps = nothing
set dApps = nothing
set oErr = nothing
set oXMLDoc = nothing
end function
Const strPath = "c:\b"
Const logFile = "c:\b\log\xmllog.xml"
dim fso, folder, xmlFiles, errs
set fso = CreateObject("Scripting.FileSystemObject")
set folder = fso.GetFolder(strPath)
xmlFiles = fileList(folder)
errs = queryXML(xmlFiles)
if len(errs) > 0 Then
prt "Errors found!"
prt "Log written to " & logFile
log errs
else
prt "No errors found!"
end if
set fso = nothing
set folder = nothing
-- Roland Hall /* This information is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. */ Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp MSDN Library - http://msdn.microsoft.com/library/default.asp
- Next message: Matthias Tacke: "Re: Collecting Windows NT 4.0 events"
- Previous message: mikn: "Re: Newbie search question"
- In reply to: Roland Hall: "Re: Loading mulitple files"
- Next in thread: Big D: "Re: Loading mulitple files"
- Reply: Big D: "Re: Loading mulitple files"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|