Re: Loading mulitple files

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Roland Hall (nobody_at_nowhere)
Date: 01/28/05


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


Relevant Pages

  • Re: Need Some Quick SBS/Exchange Help!
    ... This logfile has been damaged and is unusable. ... log-file fill pattern first appeared in sector 5452. ... Is there an antivirus program on this box that does not have the Exchange folders excluded? ... check the quarantined folder for the ..e00 log file. ...
    (microsoft.public.windows.server.sbs)
  • Re: Delete top part of file
    ... gzip logfile.1 ... If logfile is still being written to, ... don't want the/a log file to be missing at any time (e.g. avoid race ... typically preferable not to lose current log data as the rotation is ...
    (comp.unix.shell)
  • Re: Logfile
    ... The issue is that I just want to find a way to fill up the log file ... with messages generated as the process flows. ... In the procedure the message is written to the logfile and added with a ...
    (comp.databases.ms-access)
  • Re: Expect: spawned process in sleep state (log_file problem?)
    ... I can't imagine why it has anything to do with the logfile. ... script is hanging and output of truss would be helpful. ... > the xyz script ran in background. ... > want xyz to have its own log file. ...
    (comp.lang.tcl)