Rename XML files with VBScript, using XPath

穿精又带淫゛_ 提交于 2020-01-16 13:05:31

问题


I've got a VBScript that renames XML files in a directory, based on nodes values present on these XMLs.

These files need to be renamed with a "Operadora" name and a "Date". "Operadora" is an unique node, but there are several "Date" nodes on my XMLs, so I need to take the most recent "Date" as part of the file name. A friend in StackOverflow helped me out finding a way to do it to one archive, but I'm getting an error implementing it when it's about to read multiple files.

I'm getting an "Object required" error in the "nomeCerto = ..." line, after renaming the first File. I'm almost sure this "Set recentDate = Nothing" is wrong but I can't find something to change it. The code works perfectly when the "recentDate.Text" is taken out.

Follows the program:

Dim Caminho
Dim FSO
Dim FLD
Dim fil
Dim nomeErrado
Dim nomeCerto
Dim xmlDoc
Dim OrganisationInfo, Operadora, recentDate, contador, resultOperadora

Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0") 'Msxml2.DOMDocument / Microsoft.XMLDOM   
xmlDoc.Async = "False"
xmlDoc.setProperty "SelectionLanguage", "XPath"

Caminho = "C:\Users\f8057612\Desktop\Bancos\Script_Operadoras"

Set FSO = CreateObject("Scripting.FileSystemObject")        
Set FLD = FSO.GetFolder(Caminho)                            
contador = 1

For Each fil in FLD.Files
    If LCase(FSO.GetExtensionName(fil)) = "xml" Then
        xmlDoc.Load fil.Path
        nomeErrado = fil.Path

        If xmlDoc.ParseError = 0 Then

            For Each OrganisationInfo In xmlDoc.SelectNodes("//OrganisationInfo/OrganisationName")
                Operadora = OrganisationInfo.Text
            Next

            resultOperadora = Replace(Operadora, "/", "-")

            Set recentDate = Nothing

            For Each node In xmlDoc.SelectNodes("//NetworkData/RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date")
                If recentDate Is Nothing Then
                    Set recentDate = node
                ElseIf node.Text > recentDate.Text Then
                    Set recentDate = node
                End If
            Next

            nomeCerto = "IR21 - " & resultOperadora & " - " & contador & " - " & recentDate.Text & ".xml"                    ' " - " & recentDate.Text &
            'WScript.Echo "_" & nomeErrado & "_" & vbNewLine & "_" & nomeCerto & "_"

            FSO.MoveFile nomeErrado, nomeCerto
            contador = contador + 1
            resultOperadora = ""

        End If
    End If
Next

Set FLD = Nothing
Set FSO = Nothing

Can someone please help me?


回答1:


Check the value of recentDate:

WScript.Echo TypeName(recentDate)

It's most likely Nothing when the error occurs.

If you get an Object required error in the line

nomeCerto = "IR21 - " & resultOperadora & " - " & contador & " - " _
  & recentDate.Text & ".xml"

the only possible cause (AFAICS) is that recentDate is not an object. The reason for this is probably that xmlDoc.SelectNodes("//NetworkData/RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date") doesn't find any matching nodes in the XML document. Double-check the content of the file you're processing at that point.

You can mitigate the error by adding a check like this:

If recentDate Is Nothing Then
  nomeCerto = ...
  FSO.MoveFile nomeErrado, nomeCerto
End If


来源:https://stackoverflow.com/questions/21116094/rename-xml-files-with-vbscript-using-xpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!