此代码始终将 XML 文件 .xml 备份到 .backup,然后如果成功,它会保存到 _Processed.xml。希望这就是你想要的。
Private Function ProcessFilesFromXml(ByRef the_sXmlFileName As String) As Boolean
Dim nPosDot As Long
Dim sXmlFileNamePrefix As String
Dim sXmlFileNameSuffix As String
Dim oXmlDoc As MSXML2.DOMDocument
Dim oXmlMediaFileNode As MSXML2.IXMLDOMElement
Dim oXmlActionNode As MSXML2.IXMLDOMElement
On Error GoTo ErrorHandler
nPosDot = InStr(1, the_sXmlFileName, ".")
If (nPosDot) Then
sXmlFileNamePrefix = Left$(the_sXmlFileName, nPosDot - 1)
sXmlFileNameSuffix = Mid$(the_sXmlFileName, nPosDot)
Else
sXmlFileNamePrefix = the_sXmlFileName
sXmlFileNameSuffix = vbNullString
End If
' First of all, back up the XML file <XmlFileList>.xml to <XmlFileList>.backup . Overwrites existing backup file.
FileCopy the_sXmlFileName, sXmlFileNamePrefix & ".backup"
On Error GoTo ErrorHandler_ProcessingXml
' Load the Xml file <XmlFileList>.xml
Set oXmlDoc = New MSXML2.DOMDocument
oXmlDoc.Load the_sXmlFileName
' Iterate through each media file, and try to copy it.
For Each oXmlMediaFileNode In oXmlDoc.selectNodes("/ArrayOfMediaFile/MediaFile")
If Not CopyFileNameToLocalMachine(oXmlMediaFileNode.selectSingleNode("fileName").Text, oXmlMediaFileNode.selectSingleNode("filePath").Text) Then
Set oXmlActionNode = oXmlMediaFileNode.selectSingleNode("Action")
oXmlActionNode.Text = "keep"
End If
Next oXmlMediaFileNode
' Save under the name <XmlFileList>_Processed.xml
oXmlDoc.save sXmlFileNamePrefix & "_Processed" & sXmlFileNameSuffix
' Since we got here, things must have been fine.
ProcessFilesFromXml = True
Return_ProcessingXml:
'
Exit Function
ErrorHandler_ProcessingXml:
ProcessFilesFromXml = False
Resume Return_ProcessingXml
ErrorHandler:
Err.Raise Err.Number, Err.Source, Err.Description
End Function
Private Function CopyFileNameToLocalMachine(ByRef the_sFileName As String, ByRef the_sLocalPath As String)
' Your code here.
End Function