VBS 多次 REPLACE 用法

VBS multiple REPLACE usage

我为我的 RSYNC 日志做了 logEditor,他的任务是替换生成的日志文件中的状态字符串,并为最终用户创建编辑过的新文件。为此,我在 VBS 中使用 REPLACE 方法。一切正常,除了一个问题,我仍然无法解决。

当我多次使用 REPLACE 时,它只需要先替换用法,将其写入文件并忽略其他用法。但是我需要多次使用替换来替换多个状态

Function logEditor(strInputFile, strLogFileName)
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strInputFile, ForReading)
strText = objFile.ReadAll
objFile.Close

'replaceOfStatus
strNewText1 = Replace(strText, "cd+++++++++", "CDir")
'this one is going to be ignored
strNewText2 = Replace(strText, "<f+++++++++", "FILE")

Set objFile = objFSO.OpenTextFile(strLogFileName, ForWriting)
objFile.WriteLine strNewText1 'writed one
objFile.WriteLine strNewText2 'ignored one
objFile.Close
End Function
call logEditor(strInputFile, strLogFileName)

这是 rsync 日志的示例

2018/04/27 12:29:40 [792] .d..t...... texlive/
2018/04/27 12:33:31 [792] cd+++++++++ texlive/Downloads/
2018/04/27 12:33:31 [792] <f+++++++++ texlive/Downloads/Backup.zip
2018/04/27 12:33:32 [792] <f+++++++++ texlive/Downloads/ChromeSetup.exe
2018/04/27 12:33:43 [792] <f+++++++++ texlive/Downloads/test.txt
2018/04/27 12:33:43 [792] <f+++++++++ texlive/Downloads/desktop.ini

你们能帮帮我,告诉我我的逻辑哪里不好吗?

提前感谢您的所有回答。

EDIT_0: 在此方法结束时,我需要新的日志文件如下所示:

2018/04/27 12:29:40 [792] Dir texlive/
2018/04/27 12:33:31 [792] CDir texlive/Downloads/
2018/04/27 12:33:31 [792] File texlive/Downloads/Backup.zip
2018/04/27 12:33:32 [792] File texlive/Downloads/ChromeSetup.exe
2018/04/27 12:33:43 [792] File texlive/Downloads/test.txt
2018/04/27 12:33:43 [792] File texlive/Downloads/desktop.ini
Function logEditor(strInputFile, strLogFileName)
    Const ForReading = 1
    Const ForWriting = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strInputFile, ForReading)
    strText = objFile.ReadAll
    objFile.Close

    'replaceOfStatus
    strNewText1 = Replace(strText, "cd+++++++++", "CDir")
    'this one is going to be ignored
    strNewText2 = Replace(strNewText1, "<f+++++++++", "FILE") 'USE THE MODIFIED strNewText1 STRING 

    Set objFile = objFSO.OpenTextFile(strLogFileName, ForWriting, True)
'   objFile.WriteLine strNewText1 'writed one YOU DONT NEED THIS LINE
    objFile.Write strNewText2 'ignored one HERE THE ENTIRE FILE IS BEING WRITTEN
    objFile.Close
End Function

几个变化

  1. 因为你想进行多次替换 - 你需要累积地实现 - 比如先替换 CD++ 然后使用修改后的字符串替换

  2. 替换完成后,只需将最终字符串写入文件