VBS多次REPLACE使用

时间:2018-05-09 12:23:49

标签: vbscript rsync text-editor overwrite file-writing

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

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

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

1 个答案:

答案 0 :(得分:0)

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. 完成替换后,只需将最终字符串写入文件

  3. 即可