vbs代码删除文本文件中的行
以下是一个示例的VBS代码,可以删除文本文件中的特定行:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\path\to\file.txt", 1)
strContents = objFile.ReadAll
objFile.Close
arrLines = Split(strContents, vbCrLf)
strNewContents = ""
For i = 0 To UBound(arrLines)
'根据需要删除的行号进行修改
If i <> 2 Then
strNewContents = strNewContents & arrLines(i) & vbCrLf
End If
Next
Set objFile = objFSO.OpenTextFile("C:\path\to\file.txt", 2)
objFile.Write strNewContents
objFile.Close
在上面的代码中,需要修改"C:\path\to\file.txt"为要操作的文本文件的路径。在If i <> 2 Then这一行中,可以根据需要修改要删除的行号。在示例代码中,删除的是第3行(行号从0开始计算)。
请注意,此代码将会覆盖原始的文本文件,请确保在运行代码之前备份好原始文件
原文地址: https://www.cveoy.top/t/topic/iyyI 著作权归作者所有。请勿转载和采集!