首页 > 代码库 > VB 删除txt文件的最后一行

VB 删除txt文件的最后一行

Private Sub Command1_Click()
DelTxt App.Path & "\1.txt"
End Sub
Sub DelTxt(ByVal Path As String) ‘删除txt文件的最后一行
On Error GoTo eHandler:
Dim Fc As String, Fn() As String, I As Integer, Wjh As Integer
Wjh = FreeFile
Open Path For Binary As #Wjh
Fc = Space(LOF(Wjh))
Get #Wjh, , Fc
Close #Wjh
Fn = Split(Fc, vbCrLf)
ReDim Preserve Fn(UBound(Fn) - 1)
If UBound(Fn) <= 0 Then
Open Path For Output As #Wjh
Print vbNullString
Close #Wjh
Else
ReDim Preserve Fn(UBound(Fn) - 1)
Open Path For Output As #Wjh
For I = 0 To UBound(Fn)
Print #Wjh, Fn(I)
Next
Close #Wjh
End If
Exit Sub
eHandler:
Err.Clear
End Sub

VB 删除txt文件的最后一行