Edit XML with batch file

前端 未结 5 971
别那么骄傲
别那么骄傲 2021-01-06 06:41

I am wondering if there is any way to create a batch file that can edit a line in an XML document. The line would be identified by the preceding line. the idea would be as

5条回答
  •  遥遥无期
    2021-01-06 07:23

    sure, natively, you can use batch, but i recommend you to learn and use vbscript instead

    Set objFS=CreateObject("Scripting.FileSystemObject")
    strFile = WScript.Arguments.Item(0)
    strUserValue= WScript.Arguments.Item(1)
    Set objFile = objFS.OpenTextFile(strFile)
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        If  InStr(strLine,"Csetting name") >0 And _
            InStr(strLine,"BaseDirectory")> 0 And _
            InStr(strLine,"serializeAs=") > 0 Then      
            strLine=strLine & vbCrLf & "" & strUserValue & ""        
        End If 
        WScript.Echo strLine
    Loop
    

    save the script as edit.vbs and in your batch

    c:\test> cscript //nologo edit.vbs file "user value"
    

    vbscript is the best you got besides cripple batch, if you hate the idea of using other tools like gawk/sed/Python/Perl or other xml parsers/writers. Otherwise, you should consider using these better tools.

提交回复
热议问题