VBS编程:逐行读取文件,如果读出字符串中包含"aaa",则替换为"P"。其余则不变。
要求把未变的内容继续保留,替换过的内容写入原文件。我写了一段,它只写入了最后一段内容,、其余都被覆盖了。刚开始写,达人们见谅莫怪。OnErrorResumeNextDim...
要求把未变的内容继续保留,替换过的内容写入原文件。我写了一段,它只写入了最后一段内容,、其余都被覆盖了。刚开始写,达人们见谅莫怪。
On Error Resume Next
Dim path
path = "D:123.conf"
Dim fso,fread,strline,rpStrline,stResult
Set fso = createobject("scripting.filesystemobject")
'1:ForReading 2:ForWriting 8:ForAppending
Set fread = fso.OpenTextFile(path,1)
Do Until fread.atendofstream
strline=fread.readline
if instr(strline,"aaa")>0 then
rpStrline = Replace(strline,"aaa","P")
MsgBox rpStrline
else
MsgBox "nothing"
continue
end if
Loop
Set fread = fso.CreateTextFile(path,2,true)
fread.WriteLine(rpStrline)
MsgBox "书き込みしました。"
fread.closeSet fso=nothing
If Err.Number <> 0 Then
On Error GoTo 0
End If
123.conf文件内容如下:
111111111
222222222
333333333
444444444
555555555
aaa111aaa
1aaa1aaa1
22aaa2aaa
aaa333aaa
4aaa5aaa5
66aaa6aaa
大家帮帮忙吧!坐等。。。谢谢大家了! 展开
On Error Resume Next
Dim path
path = "D:123.conf"
Dim fso,fread,strline,rpStrline,stResult
Set fso = createobject("scripting.filesystemobject")
'1:ForReading 2:ForWriting 8:ForAppending
Set fread = fso.OpenTextFile(path,1)
Do Until fread.atendofstream
strline=fread.readline
if instr(strline,"aaa")>0 then
rpStrline = Replace(strline,"aaa","P")
MsgBox rpStrline
else
MsgBox "nothing"
continue
end if
Loop
Set fread = fso.CreateTextFile(path,2,true)
fread.WriteLine(rpStrline)
MsgBox "书き込みしました。"
fread.closeSet fso=nothing
If Err.Number <> 0 Then
On Error GoTo 0
End If
123.conf文件内容如下:
111111111
222222222
333333333
444444444
555555555
aaa111aaa
1aaa1aaa1
22aaa2aaa
aaa333aaa
4aaa5aaa5
66aaa6aaa
大家帮帮忙吧!坐等。。。谢谢大家了! 展开
展开全部
你应该定义一个变量用于收集每一行读取出来的文本(无需处理和处理完成的),然后,在循环结束后将这个变量的值写入文件.关键代码改后如下:
Dim rpStrlines
Do Until fread.atendofstream
strline=fread.readline
if instr(strline,"aaa")>0 then
rpStrline = Replace(strline,"aaa","P")
rpStrlines = rpStrlines & rpStrline & vbcrlf
MsgBox rpStrline
else
rpStrlines = rpStrlines & strline & vbcrlf
MsgBox "nothing"
end if
Loop
Set fread = fso.CreateTextFile(path,2,true)
fread.WriteLine(rpStrlines)
Dim rpStrlines
Do Until fread.atendofstream
strline=fread.readline
if instr(strline,"aaa")>0 then
rpStrline = Replace(strline,"aaa","P")
rpStrlines = rpStrlines & rpStrline & vbcrlf
MsgBox rpStrline
else
rpStrlines = rpStrlines & strline & vbcrlf
MsgBox "nothing"
end if
Loop
Set fread = fso.CreateTextFile(path,2,true)
fread.WriteLine(rpStrlines)
展开全部
On Error Resume Next
Dim path , strAllText
path = "c:\123.conf"
Dim fso,fread,strline,rpStrline,stResult
Set fso = createobject("scripting.filesystemobject")
'1:ForReading 2:ForWriting 8:ForAppending
Set fread = fso.OpenTextFile(path,1)
strAllText = ""
Do Until fread.atendofstream
strline=fread.readline
if instr(strline,"aaa")>0 then
rpStrline = Replace(strline,"aaa","P")
strAllText = strAllText & rpStrline & chr(13) & chr ( 10)
MsgBox strline & "->" & rpStrline
else
strAllText = strAllText & strline& chr(13) & chr ( 10 )
MsgBox "nothing(" & strline & ")"
'continue
end if
Loop
Set fread = fso.CreateTextFile(path,2,true)
fread.Write strAllText
MsgBox "书き込みしました。"
msgbox strAllText
fread.closeSet fso=nothing
If Err.Number <> 0 Then
On Error GoTo 0
End If
Dim path , strAllText
path = "c:\123.conf"
Dim fso,fread,strline,rpStrline,stResult
Set fso = createobject("scripting.filesystemobject")
'1:ForReading 2:ForWriting 8:ForAppending
Set fread = fso.OpenTextFile(path,1)
strAllText = ""
Do Until fread.atendofstream
strline=fread.readline
if instr(strline,"aaa")>0 then
rpStrline = Replace(strline,"aaa","P")
strAllText = strAllText & rpStrline & chr(13) & chr ( 10)
MsgBox strline & "->" & rpStrline
else
strAllText = strAllText & strline& chr(13) & chr ( 10 )
MsgBox "nothing(" & strline & ")"
'continue
end if
Loop
Set fread = fso.CreateTextFile(path,2,true)
fread.Write strAllText
MsgBox "书き込みしました。"
msgbox strAllText
fread.closeSet fso=nothing
If Err.Number <> 0 Then
On Error GoTo 0
End If
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询