用vb怎样去掉一个文件夹中多个文本文件最后一个句号后面的内容?

一个文件夹中有很多个txt文本文件。这些文件最后都有个乱码,这些乱码的规律是都位于文本文件最后一个标点符号——句号的后面。如何去掉这些乱码昵?

第1个回答  2011-10-25
每个txt文本文件里有几个段落?

Function fileList()
'添加 commondialog1
'添加 file1, 可设为不可见 也就是只当作数组

On Error GoTo err
With CommonDialog1
.DialogTitle = "请指定文件夹"
.Filter = "文本文件(*.txt)|*.txt"
.ShowOpen
File1.FileName = .FileName
End With
File1.Pattern = "*.txt"
File1.Path = File1.Path '此句妙极!!为我所创!!
Exit Function
err:
MsgBox "您没有选择文件或者文件夹中没有txt文件"
End Function
'写
Sub wirteTxt(namePath As String)
Open namePath For Output As #1
Print #1, , Text1.Text
Close #1
End Sub

'读
Function lineRead(namePath As String) As String
'多行数据
Dim strLine As String
Dim strBox As String
Dim zzc As Variant

Open namePath For Input As #1
Do Until EOF(1)
Line Input #1, strLine
strBox = strBox + strLine + vbCrLf
Loop

zzc = Split(strBox, vbCrLf)

Dim i As Integer, j As Integer
Dim lishi As Variant

i = UBound(zzc) - 1
lishi = Split(zzc(i), "。")

j = UBound(lishi)
lishi(j) = ""
MsgBox j
zzc(i) = Join(lishi, "。")

Text1.Text = Join(zzc, vbCrLf)
Close #1

wirteTxt File1.List(ListIndex)

End Function

Private Sub Command1_Click()
fileList

End Sub

Private Sub File1_Click() '这只是修改一个文件,你可以用循环修改文件夹下所有的txt文件
lineRead File1.List(ListIndex)
End Sub

张志晨 :如果出现下界超标问题,就是你的txt里有空行。追问

txt里有空行,而且每个txt文件的段落多少不一

第2个回答  2011-11-01
'添加drivelistbox,dirlistbox,filelistbox,commamdbutton各一个
Private Sub Command1_Click()
Dim i As Integer
For i = 0 To File1.ListCount - 1
File1.ListIndex = i
If Right(File1.FileName, 3) = "txt" Then zhzsucyg (Dir1.Path & IIf(Right(Dir1.Path, 1) = "\", "", "\") & File1.FileName)
Next
End Sub

Private Sub Dir1_Change()
File1.Path = Dir1.Path & IIf(Right(Dir1.Path, 1) = "\", "", "\")
End Sub

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Function zhzsucyg(myFile As String)
Dim myStr As String, n As Integer
Open myFile For Binary As #1
myStr = Input(LOF(1), 1)
Close
myStr = StrReverse(myStr)
n = InStr(1, myStr, "。") '如果最后的句号是“.”,请把“。”换成“.”
myStr = Mid(myStr, n + 1)
myStr = StrReverse(myStr)
Open myFile For Output As #1
Print #1, myStr
Close
End Function追问

我用你的代码测试,包含最后一个句号后面的内容都去掉了。最后一个句号还是要保留的啊,只需要去掉它后面的内容。

追答

把myStr = Mid(myStr, n + 1)
改为myStr = Mid(myStr, n )

本回答被提问者采纳
第3个回答  2011-10-14
你只需要知道这个标志位,然后用函数把它们隔离开来,之后把需要得读出来就可以了。追问

请写出代码

第4个回答  2011-10-13
读进来,处理,写回去追问

请细点,好吗/

相似回答