虚心求教
小弟虚心请教大家,实现TXT温暖件中的查找和替换功能
我用VB做的如下:
Dim FileNames() As String '文件名列表数组,下标从1开始
Dim FileCount As Long '文件名列表中的文件名总数
Private Sub cmdChooseFiles_Click()
Dim i As Long, y As Long, z As Long
With CommonDialog1
.Flags = cdlOFNAllowMultiselect '允许选择多个文件
.Filter = "TXT Files|*.txt" '只显示TXT文件,可根据需要改变
.MaxFileSize = 10000 'FileName属性的最大长度,可根据需要改变
.ShowOpen '显示"打开文件"对话框
If .FileName = "" Then
'如选择了"取消"按钮,则将FileCount置为0并返
FileCount = 0
Exit Sub
End If
.FileName = .FileName & " " '在FileName最后添加一个空格,以便处理
'以下从FileName属性中逐个读取文件名,并放入FileNames数组中
z = 1
y = 0
For i = 1 To Len(.FileName)
i = InStr(z, .FileName, " ")
If i = 0 Then
Exit For
End If
ReDim Preserve FileNames(y)
FileNames(y) = Mid(.FileName, z, i - z)
'从文件的第I个字符串中开始取出I-Z个字符串
If y > 0 Then '如有多个文件,则在每个文件名前加上完整的路径
FileNames(y) = FileNames(0) & "\" & FileNames(y)
End If
z = i + 1
y = y + 1
Next i
If y = 1 Then '如FileName只包含一个文件名,则将其赋给FileName(1)
'并将FileCount置为1
ReDim Preserve FileNames(1)
FileNames(1) = FileNames(0)
FileCount = 1
Else '否则根据文件数确定FileCount的值
FileCount = y - 1
End If
End With
End Sub
Private Sub cmdOk_Click()
Dim i As Long
Dim SearchString As String '搜索字符串
Dim ReplaceSring As String '替换字符串
Dim StratPosition As Long '搜索字符串在文件中出现的位置
Dim SearchLength As Long '搜索字符串长度
SearchString = Trim(textSearch) '删除字符串首部和尾部的空格
ReplaceSring = Trim(textReplace)
SearchLength = Len(SearchString)
With RichTextBox1
For i = 1 To FileCount '以下依次处理FileNames数组中的文件
RichTextBox1.LoadFile FileNames(i), rtfText '读入文件
StartPosition = .Find(SearchString) '寻找搜索字符串
If StartPosition <> -1 Then '如果找到,则执行替换操作
.SelStart = StartPosition '开始位置
.SelLength = SearchLength
.SelText = ReplaceString '替换字符串
.SaveFile FileNames(i), rtfText '写入文件
End If
Next i
End With
MsgBox "转换结束!", vbExclamation
End Sub
但是最后还是转换不了啊 请高手帮忙啊 谢谢了