最近有个需求,需要替换配置文件中的一个字段,因为配置文件较多,所以想着写工具来做。
一开始准备用批处理的,奈何需要替换的内容中有特殊字符,如果没有还是挺方便的。既然批处理不行就转战vba了。代码不复杂,就是记录下,以后可能有用
rootPath="NCBS/"
resulePath="Result/"
logPath="Run.log"
oldStr= ""
newStr=""
Set logfso = CreateObject("Scripting.FileSystemObject")
Set logfile = logfso.CreateTextFile(logpath,True)
Set fso = CreateObject("Scripting.FileSystemObject")
Set folders = fso.GetFlolder(rootPath)
Set files = folders.files
num=0
startDate=Now()
For Each file In files
If instr(file.name,"_system_") > 0 Then
logfile.WriteLine("开始处理文件[" & file.name & "]")
Set stream = fso.opentextfile(rootPath & file.name,1)
content = stream.readall()
stream.close()
content= Replace(content,oldStr,newStr)
Set stream = fso.CreateTextFile(resulePath & file.name,True)
stream.write(content)
stream.close()
num = num +1
End If
Next
endDate = Now()
timeDiff=DateDiff("s",startDate,endDate)
logfile.WriteLine("共处理[" & num & "]个文件,耗时[" & timeDiff &"]s")
Set fso = Nothing
Set logfso = Nothing
到这里就结束了