先读出文件到字串,用replace函数替换,写回文件
dim buff() as byte
dim i as long
dim mfile as string
mfile="c:\123.txt"
i=filelen(mfile)
redim buff(i-1)
open mfile for binary as #1
get #1,,buff
close #1
dim s as string
s=strconv(buff,vbunicode)
dim s1 as string
dim s2 as string
s1="123"+vbcrlf+"456"
s2="123"+vbcrlf+"ccc"+vbcrlf+"456"
s=replace(s,s1,s2)
kill mfile
open mfile for binary as #1
put #1,,s
close #1
'重新声明字符数组的长度
dim str() as string
str=sprit(text1.text,vb.crlf)
dim strlength as integer=ubound(str)
dim linecount as string
dim i as integer
for i=0 to ubound(str)
dim s as string
dim j as integer
dim k as integer
s=str(i)
j=instr(s,"abc")
if j<>0 then
msgbox"你找的文本abc存在!" & "在第" & cstr(i+1) & "行。" '确定abc的位置
for k=i+1 to ubound(str)
dim s1 as string
s1=str(k)
dim m as integer
m=instr(s1,"123")
if m<>0 then
msgbox"你找的文本123存在!" & "在第" & cstr(k+1) & "行。" '确定123的位置
redim preserve str(strlength+1) '重新声明字符数组的长度
str(i+1)="ccc"
end if
next
end if
text
dim str() as string
str=sprit(text1.text,vb.crlf)
dim linecount as string
dim i as integer
for i=0 to ubound(str)
dim s as string
dim j as integer
dim k as integer
s=str(i)
j=instr(s,"abc")
if j<>0 then
msgbox"你找的文本abc存在!" & "在第" & cstr(i+1) & "行。" '确定abc的位置
for k=i+1 to ubound(str)
dim s1 as string
s1=str(k)
dim m as integer
m=instr(s1,"123")
if m<>0 then
msgbox"你找的文本123存在!" & "在第" & cstr(k+1) & "行。" '确定123的位置
……
'再次插入字符ccccc
'代码略
……
end if
next
end if
text
Function WriteLineToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.WriteLine "Hello world!"
f.WriteLine "VBScript is fun!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
WriteLineToFile = f.ReadAll
End Function
Function ReadLineTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
MyFile.WriteLine "Hello world!"
MyFile.WriteLine "The quick brown fox"
MyFile.Close
Set MyFile = fso.OpenTextFile("c:\testfile.txt", ForReading)
ReadLineTextFile = MyFile.ReadLine ' Returns "Hello world!"
End Function