2011-10-31 48 views
0

如何用替換的文本保存所有文本。
在現在僅保存替換的文本VBScript中的正則表達式

我想改變TXT僅9752951c-0392-71e1-01a3- ac10016b0000 的1.txt

text ... 
     <URL>http://bs.com/opr-console/rest/9.10/event_list/9752951c-0392-71e1-01a3- ac10016b0000</URL>   
      <method>PUT</method> 
      <auth-methods>DIGEST</auth-methods> 
      <auth-preemptive>true</auth-preemptive> 
      <auth-username>admin</auth-username> 
      <auth-password>rO0ABXQABWFkbWlu</auth- 
      .....bla-bla-la.. 

vbs腳本:

Dim objExec, objShell, objWshScriptExec, objStdOut, objArgs, ReplaceWith 
Const ForReading = 1 
Const ForWriting = 2 
Set objArgs = WScript.Arguments 
strID = Trim(objArgs(0)) 
Set objRegEx = CreateObject("VBScript.RegExp") 
objRegEx.Pattern = "^<URL>http://bsmgw.bms.consulting.com/opr-console/rest/9.10/event_list/" 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\test\1\1.txt", ForReading) 

Do Until objFile.AtEndOfStream 
    strSearchString = objFile.ReadLine 
    Set colMatches = objRegEx.Execute(strSearchString) 
    If colMatches.Count > 0 Then 
     For Each strMatch In colMatches 
      Wscript.Echo strSearchString 

      st = Mid(strSearchString, 71, 36) 
      WScript.Echo st 
      strNewFileName = Replace(strSearchString, st, strID) 
     Wscript.Echo strNewFileName 
     objFile.Write 
     Next 
    End If 
Loop 
objFile.Close 
objFile.Close 
objFile.Close 

回答

0

個人,我會使用XML解析器改變您的文件:

' load the document into an object 
Dim xmldoc: set xmldoc = CreateObject("MSXML2.DomDocument") 
xmldoc.async = false 
xmldoc.setProperty "SelectionLanguage", "XPath" 
xmldoc.load "C:\test\1\1.txt" 

' get the URL node (look up XPath - i'm assuming that there is a single node called URL) 
dim urlnode: set urlnode = xmldoc.selectSingleNode("//URL") 
' replace the innerText of the URL node with your replacement text 
urlnode.text = strID 

'save your document 
xmldoc.Save "C:\test\1\2.txt"