使用 XPath 使用 VBScript 重命名 XML 文件 [英] Rename XML files with VBScript, using XPath
问题描述
我有一个 VBScript,可以根据这些 XML 上存在的节点值重命名目录中的 XML 文件.
I've got a VBScript that renames XML files in a directory, based on nodes values present on these XMLs.
这些文件需要使用Operadora"名称和Date"进行重命名.Operadora"是一个独特的节点,但我的 XML 中有几个日期"节点,所以我需要将最近的日期"作为文件名的一部分.StackOverflow 中的一位朋友帮助我找到了一种方法来对一个存档执行此操作,但是当它即将读取多个文件时,我在实现它时遇到错误.
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.
重命名第一个文件后,我在nomeCerto = ..."行中收到需要对象"错误.我几乎可以肯定这个设置最近日期 = 无"是错误的,但我找不到可以改变它的东西.取出recentDate.Text"后,代码完美运行.
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.
遵循程序:
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
有人可以帮我吗?
推荐答案
检查recentDate
的值:
WScript.Echo TypeName(recentDate)
发生错误时很可能Nothing
.
如果您在行中收到 Object required
错误
If you get an Object required
error in the line
nomeCerto = "IR21 - " & resultOperadora & " - " & contador & " - " _
& recentDate.Text & ".xml"
唯一可能的原因 (AFAICS) 是 recentDate
不是对象.其原因可能是 xmlDoc.SelectNodes("//NetworkData/RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date")
在 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
这篇关于使用 XPath 使用 VBScript 重命名 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!