【问题标题】:How to replace text and symbols in a txt file?如何替换txt文件中的文本和符号?
【发布时间】:2015-05-21 01:09:16
【问题描述】:

我有一个web.config 文件,要更改设置,我必须手动进入并不断更改代码。

到目前为止,我发现的是我正在尝试调整的示例脚本:

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

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Jim ", "James ")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close 

我的问题是我需要更改符号而不是文本,我认为这搞砸了。

所以上面说JamesJim的地方,我的数据要改了:

"<component type="Satsuma.Connectivity.Services.Contracts.Mocks.CallCredit.CallCreditCallValidateServiceProxyMock, Satsuma.Connectivity.Services.Contracts.Mocks" service="Satsuma.Connectivity.Services.Contracts.CallCredit.ICallCreditCallValidateService, Satsuma.Connectivity.Services.Contracts" /> ",

到这里:

"<!--<component type="Satsuma.Connectivity.Services.Contracts.Mocks.CallCredit.CallCreditCallValidateServiceProxyMock, Satsuma.Connectivity.Services.Contracts.Mocks" service="Satsuma.Connectivity.Services.Contracts.CallCredit.ICallCreditCallValidateService, Satsuma.Connectivity.Services.Contracts" />-->",)

反之亦然。如果你看,只有开始和结束符号(例如&lt;!--&lt;&gt;--&gt;)需要编辑。

【问题讨论】:

  • 请注意 - 我必须搜索完整的行我不能只搜索和替换符号
  • 请不要在 cmets 中发布复杂的代码 sn-ps,因为这样会变得有点不可读; edit 你的问题。对于Line 2 Char 1 invalid procedure call or argument 错误:ForReadingForWriting 常量都不是VBScript 中固有的not,因此您需要明确定义它们,例如如下:Const ForWriting = 2, ForReading = 1

标签: vbscript


【解决方案1】:

可以像这样进行简单的字符串替换(出于可读性原因,缩写了广泛的属性值):

srch = "<component type=""Sats...ocks"" service=""Sats...acts"" />"
repl = "<!--" & srch & "-->"
strNewText = Replace(strText, srch, repl)

请注意,VBScript 字符串中的嵌套双引号必须加倍才能转义。未转义的双引号会过早终止字符串,您很可能会遇到语法错误。

话虽如此,用字符串替换修改web.config 文件是个坏主意™,因为它们是 XML 文件,而在 XML 中,以下任何一种表示法都没有区别:

<component type="foo" service="bar" />

<component    type='foo'    service='bar'    />

<component
  type="foo"
  service="bar"
/>

<component type="foo" service="bar"></component>

您真正想做的是使用XML parser。这样的事情应该做(代码从this answer无耻地窃取):

config = "C:\path\to\your\web.config"

Set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.async = False
xml.load config

If xml.parseError <> 0 Then
  WScript.Echo xml.parseError.reason
  WScript.Quit 1
End If

Set node = xml.selectSingleNode("//component[@type='Sats...ocks']")
Set comment = xml.createComment(node.xml)
node.parentNode.replaceChild(comment, node)

xml.save config

【讨论】:

    【解决方案2】:

    所有"inner"双引号应为""加倍""如下:

    replWhat = "<component type=""Satsuma..Mocks"" service=""Satsuma..Contracts"" />"
    '                           ^               ^          ^                   ^
    replWith = "<!--" & replWhat & "-->" 
    
    strNewText = Replace(strText, replWhat, replWith, 1, -1, vbTextCompare)
    
    'and vice versa'
    strOldText = Replace(strNewText, replWith, replWhat, 1, -1, vbTextCompare)
    

    在给定的示例中,文本被缩短以使命令保持合理的宽度以提高可读性。

    阅读VBScript Language Reference 以了解Replace 函数的附加, 1, -1, vbTextCompare 参数的含义。

    【讨论】:

    • 我必须在 web.config 文件中搜索整行代码,因为有很多代码行包含我不需要替换的符号,所以这不起作用我假设它将替换文件中的所有符号。 srch = "" repl = "" strNewText = Replace( strText, srch, repl)
    • 这里的... 并不意味着任何类型的wildcard character 用于VBScript 解释器,而是用作ellipsis (in linguistic sense) 以提高人类 的可读性。但是,请遵循 @Ansgar 并使用 XML 解析器...
    • 谢谢,我有一些想法可以解决这个问题,首先我想让这个简单的脚本工作,但是当我尝试运行它时,我看到 Line 2 Char 1 invalid procedure call or argument Set objFSO = CreateObject("Scripting.FileSystemObject") 设置 objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt", ForReading) strText = objFile.ReadAll objFile.Close strNewText = Replace(strText, "Jim ", "James ") 设置 objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Textt.txt", ForWriting) objFile.WriteLine strNewText objFile.Close
    猜你喜欢
    • 2015-12-13
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 2018-11-30
    • 1970-01-01
    • 2019-07-18
    相关资源
    最近更新 更多