\'功能:删除、替换文本中一行,或者插入内容到文本中某一行
\'作者: soho_andy (冰)
\'参数:
\'strSourceFile 原始文件完整名
\'strTargetFile 生成新文件的完整名
\'intRow 操作的行数
Sub 操作文件中一行(strSourceFile As String, strTargetFile As String, intRow As Long)
Dim filenum As Integer
Dim fileContents As String
Dim fileInfo() As String
Dim i As Integer
Dim j As Integer
filenum = FreeFile
Open strSourceFile For Binary As #filenum
fileContents = Space(LOF(filenum))
Get #filenum, , fileContents
Close filenum
fileInfo = Split(fileContents, vbCrLf)
\'取出源文件行数,按照回车换行来分隔成数组
filenum = FreeFile
If Dir(strTargetFile, vbNormal) <> \"\" Then
Kill strTargetFile
End If
Dim Filestr() As String
\'删除一行代码块
Open strTargetFile For Append As #filenum
\'循环每一行
For i = 0 To UBound(fileInfo) - 1
If i <> intRow - 1 Then
Print #filenum, fileInfo(i)
End If
Next
Close #filenum
\'替换一行代码块
Open strTargetFile For Append As #filenum
\'循环每一行
For i = 0 To UBound(fileInfo) - 1
If i = intRow - 1 Then
Print #filenum, \"你要替换进去的内容\"
End If Next
Close #filenum
\'插入一行代码块
Open strTargetFile For Append As #filenum
\'循环每一行
For i = 0 To UBound(fileInfo) - 1
If i = intRow - 1 Then
Print #filenum, \"你要插入到这行的内容\"
Print #filenum, fileInfo(i) \'保留原来的行,位置后移一位
End If
Next
Close #filenum
MsgBox \"完毕\"
End Sub
本文来自 280文秘网(https://www.it280.com),转载请保留网址和出处
【VB操作文本文件的一个实例】相关文章:
2025年外省高中学业水平合格性考试成绩转入江苏认定的通告公布03-23
Vb中控件的自动排列03-23
写未来的作文300字03-23
Our Mother Is the Same 我们的妈妈是一样的03-23
中国大地保险公司实习报告03-23
小学父爱的作文600字03-23
荷花作文600字03-23
07年4月北师大“中国古代文学史(一)”串讲(5)03-23
寓言作文300字03-23
汪涵讲述小时候成长经历:鸡毛掸子的故事03-23
关于清明节的英语作文:清明节03-23
