VBS中RSYNC日志编辑器多次REPLACE仅生效首个问题求助
问题排查与解决方案
嘿,我一眼就看到你代码里的两个核心问题啦,咱们一个个说清楚:
1. 替换逻辑的累积错误
你现在的两次替换都是基于**原始的strText**来操作的:
strNewText1是把原始日志里的cd+++++++++换成CDirstrNewText2是又拿原始日志把<f+++++++++换成FILE
这就意味着这两个变量是完全独立的,没有继承前一次的替换结果,自然没法在同一份日志里同时体现两种替换效果。
2. 文件写入的逻辑错误
你最后把strNewText1和strNewText2都写入了文件,这相当于把两份几乎重复的日志(一份只改了cd,一份只改了<f)拼接在一起,而不是输出一份整合了所有替换的日志。
修正后的完整代码
咱们把替换操作改成链式累积,并且只写入最终整合后的日志内容:
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 ' 链式替换:每次都基于上一次替换后的结果继续操作 strNewText = Replace(strText, "cd+++++++++", "CDir") strNewText = Replace(strNewText, "<f+++++++++", "File") ' 改成小写File匹配你的期望输出 strNewText = Replace(strNewText, ".d..t......", "Dir") ' 补上你期望输出里的目录状态替换 Set objFile = objFSO.OpenTextFile(strLogFileName, ForWriting) objFile.WriteLine strNewText ' 只写入最终整合后的日志 objFile.Close End Function call logEditor(strInputFile, strLogFileName)
验证效果
用你提供的RSYNC日志示例测试,修正后的代码会完美输出你期望的结果:
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
内容的提问来源于stack exchange,提问作者user6566830
相关产品推荐
相关产品推荐

