读取xml文件,查找和编辑条目,并保存为原始文件

时间:2012-06-15 17:59:52

标签: vbscript

我正在通过查询将XML文件加载到SQL服务器中,问题是有一个10x10表,它有多个名称空间问题。我想我可以用一些vbscript解决这个问题,到目前为止这是我的代码;

Dim FSO, FLD, FIL

Dim strFolder

strFolder = "C:\Documents and Settings\Administrator\Desktop\Tests"

Set FSO = CreateObject ("Scripting.FileSystemObject")

Set FLD = FSO.GetFolder (strFolder)


For Each FIL in FLD.Files

Set objFS = CreateObject("Scripting.FileSystemObject")

i=1

Set objFile = objFS.OpenTextFile(FIL)

Do Until objFile.AtEndOfStream

    strLine = objFile.ReadLine

    If i=1 and InStr(strLine,"Data")> 0 Then

        strLine = Replace(strLine,"Data","Data1")

i=i+1

ElseIf i=2 and InStr(strLine,"Data")> 0 Then

        strLine = Replace(strLine,"Data","Data1")

i=i+1

ElseIf i=3 and InStr(strLine,"Data")> 0 Then

        strLine = Replace(strLine,"Data","Data2")

i=i+1

ElseIf i=4 and InStr(strLine,"Data")> 0 Then

        strLine = Replace(strLine,"Data","Data2")

i=i+1

ElseIf i=5 and InStr(strLine,"Data")> 0 Then

        strLine = Replace(strLine,"Data","Data3")

i=i+1

ElseIf i=6 and InStr(strLine,"Data")> 0 Then

        strLine = Replace(strLine,"Data","Data3")

i=i+1

ElseIf i=7 and InStr(strLine,"Data")> 0 Then

        strLine = Replace(strLine,"Data","Data4")

i=i+1

ElseIf i=8 and InStr(strLine,"Data")> 0 Then

        strLine = Replace(strLine,"Data","Data4")

i=i+1

ElseIf i=9 and InStr(strLine,"Data")> 0 Then

        strLine = Replace(strLine,"Data","Data5")

i=i+1

ElseIf i=10 and InStr(strLine,"Data")> 0 Then

        strLine = Replace(strLine,"Data","Data5")

i=i+1

ElseIf i=11 and InStr(strLine,"Data")> 0 Then

        strLine = Replace(strLine,"Data","Data6")

End If


    WScript.Echo strLine

Loop

next

When I run this via command prompt:

cd c:\

cscript /nologo Script.vbs >newfile

ren newfile example.xml

保存的文件example.xml是完美的 - 带有if then语句的脚本的中心部分可以正常工作。但是,只有在“tests”文件夹中我只存储一个文件时,它才能正常工作。如果我存储多个文件,并以相同的方式使用cmd,它将保存所有这些文件。我需要完成大约3,000个文件,我想要做的是在每个.xml文件上运行脚本,然后将其保存为具有相同名称的新文件(覆盖到新位置)文件夹。我如何使用相同的名称保存它们但是使用编辑?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

这是什么,某种混淆比赛?我建议你按如下方式重写你的脚本:

strFolder = "C:\Documents and Settings\Administrator\Desktop\Tests"
strFolderResult = "C:\Documents and Settings\Administrator\Desktop\Tests\Result"

Set FSO = CreateObject("Scripting.FileSystemObject")
For Each f In FSO.GetFolder(strFolder).Files
    i = 1
    Set inFile = f.OpenAsTextStream(1)
    Set outFile = FSO.CreateTextFile(FSO.BuildPath(strFolderResult, f.Name), true)
    Do Until inFile.AtEndOfStream
        strLine = inFile.ReadLine
        If InStr(strLine, "Data") > 0 Then
            If i < 12 Then i = i + 1
            strLine = Replace(strLine, "Data", "Data" & i \ 2)
        End If
        outFile.WriteLine(strLine)
    Loop
    inFile.Close
    outFile.Close
Next