我想读一个完整的XML文件的文件夹,并更改参考号为特定的格式,它是今天的日期(年月月日日),缩写,8位数字的参考起始于00000001
例如120815AB00000001然后120815AB00000002等每个文件都有一个参考号码。 它被封闭在<CPAReferenceNumber>标记。
我使用Excel和VBA来读取文件和更改相关领域。 (下面它改变“这”成在代码'这现在)的基准被设置为默认值
此代码的工作在一个单独的文件,并作出正确的改变。 这些文件具有随机的名字,也没有命名约定的地方。 我无法扩大这一点在文件夹中的所有XML文件。 任何帮助将不胜感激。
Sub ReplaceStringInFile()
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
sFileName = "c:\Search and Replace Files\blah.XML"
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
sTemp = Replace(sTemp, "THIS", "THAT")
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
End Sub
帮助后,我把它修改为下面的代码,但是,这是导致错误:sFileName是断章取义。 文本更改不会应用到XML文件。
Sub ReplaceStringInFile()
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
Const sSearchString As String = "c:\blah\*.xml"
Const directoryString As String = "c:\blah\"
iFileNum = FreeFile
sFileName = Dir(sSearchString)
Do While sFileName <> ""
Open directoryString & sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
sTemp = Replace(sTemp, "IDNUMBER", "******SUCCESS!!!!!!******")
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
Debug.Print "Do something with file named " & sFileName
sFileName = Dir()
Loop
End Sub