Re: Loading mulitple files

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

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


Date: Thu, 27 Jan 2005 21:13:15 -0600


"Big D" wrote in message news:%23HHilbJBFHA.1992@TK2MSFTNGP10.phx.gbl...
: Sorry about changing Application Name values. I was messing with on my
side.
:
: Yes ADS Version will always be present along with the other application
: names. The names will not change. The item that will change will be the
: Version Number. This Number will change when I send Updates. Therefore
when
: I send updates out I have created a script that will update the version
: field when I send softeware. Afterwards I pull all the xml files to a
: directory and compare against the template.
:
: Hopefully this helps!

Yes it does, thank you. XML is still very new to me but this is good
practice to help me with my application. It may not be the most elegant
solution but it is functional.

My directories:
XML files: c:\b
Log file: c:\b\log

Change these to what you want or modify the script to pass one or both of
them as arguments.

What it does:
It looks in the XML files path and grabs all files that end with .xml.
It then opens each one to see if the version matches the template.
If there is a conflict, it logs the filename, name and version and writes it
to the log.
The log is an XML file. Each time you run this, it logs the date and time
it was run, if errors exists so you know when the version was checked and
what the result was.

I incorporated Torgier's Dictionary script into it. Thank you Torgier.

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, logPath, logfile, logtext, arrLogs, arrText
  dim appNode, i, iMax, pi, root, node, att, text
  logPath = "\log\"
  logFile = strPath & logPath & "xmllog.xml"
  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
  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
    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
  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"
dim fso, folder, xmlFiles, errs

set fso = CreateObject("Scripting.FileSystemObject")
set folder = fso.GetFolder(strPath)

xmlFiles = fileList(folder)
errs = queryXML(xmlFiles)
log errs

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: Loading mulitple files
    ... Afterwards I pull all the xml files to a ... dim logXML, logPath, logfile, logtext, arrLogs, arrText ... : set att = logXML.createAttribute ...
    (microsoft.public.scripting.vbscript)
  • Re: Creating and formatting raw text in XML file via VBScript
    ... dim logXML, logtext, arrLogs, arrText ... set att = logXML.createAttribute ... set node = logXML.documentElement.lastChild ... Set oApps = oXMLDoc.DocumentElement.SelectNodes ...
    (microsoft.public.scripting.vbscript)
  • ASP.Net app cannot read XML with Anonymous Authentication disabled
    ... real simple XML files from it's local directory. ... Dim ProjReader As XmlReader = Nothing ... 'Get XML Data for Projector and load in Projector Dropdown ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • xml 2 sql
    ... Ok here is what I am trying to accomplish I have 12 xml files that I want to ... Dim aXMLFiles As Array ... Step 3 Connect to the sqlce database ... Dim SqlCeEngine As SqlCeEngine ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Importing XML into SQL using DTS
    ... >I need to import large XML files into an SQL table. ... > Dim objXMLDOM ... > Dim objADORS ...
    (microsoft.public.sqlserver.xml)