vb 批量处理TXT文件 怎样处理1到10 把里面的3也改成1到10

2024-11-25 04:02:05
推荐回答(3个)
回答1:

Dim s() As String, s1() As String, i As Integer
Open "1.txt" For Binary As #1 'txt文件名自己改
s = Split(Input(LOF(1), #1), vbCrLf)
Close #1
For i = 0 To UBound(s)
s1 = Split(s(i), "=")
If UBound(s1) = 1 Then
s1(1) = Replace(s1(1), "3", s1(0))
s(i) = Join(s1, "=")
End If
Next
Open "1.txt" For Binary As #1 'txt文件名自己改
Put #1, , Join(s, vbCrLf)
Close #1

回答2:

用Replace函数
格式:
Replace(被处理的字符串,要被替换的子字符串,新的字符串)
例如:
Replace("123456","3","New")
结果是:"12New456"
你的问题的思路:
读取文件
打开文件,做一个循环从1 到10
第i行,用csre(i)去替换“3”

这里有个问题,因为第3行最前有个3也要被替换了,要作特殊处理,方法很多的。

回答3:

Private Sub Command1_Click()
Open App.Path & "\1.txt" For Input As #1
Open App.Path & "\2.txt" For Output As #2
Dim TextLine As String
Dim Lin As String
For i = 1 To 10,注意文件行数不能小于10,可以预读行数作为终值。
Line Input #1, TextLine
TextLine = Replace(TextLine, 3, i)
Print #2, TextLine
Next i
Close #1
Close #2
End Sub
经过验证,完全可行。