我有一个带有数字的文本文件,我必须删除给定数字之前的行。
45048
67113.88
74484
597.6
1945.65
8714.5
32085.9
741.12
1721.39
35266.7
8260.71
23635.8
40487.5
40702.18
29544.74
110000
810000
3161000
29201.91
33000
所以我必须删除编号110000之前的行。或者在另一天,我必须在1721.39之前删除这些行。我不知道如何在用户输入值之前进行删除。如果用户提示25 ...
,则只删除前25行Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, " ", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "forward", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, ".", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, " ", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.Readline
strLine = Trim(strLine)
If Len(strLine) > 0 Then
strNewContents = strNewContents & strLine & vbCrLf
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.Write strNewContents
objFile.Close
答案 0 :(得分:2)
尝试使用此项删除行数,然后尝试自己。
Const FOR_READING = 1
Const FOR_WRITING = 2
strFileName = "C:\txt.txt"
NumberOfLinesToBeDelete=inputbox("How many lines want to delete from beginning","Delete","")
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName, FOR_READING)
strContents = objTS.ReadAll
objTS.Close
arrLines = Split(strContents, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)
For i=0 To UBound(arrLines)
If i > (NumberOfLinesToBeDelete - 1) Then
objTS.WriteLine arrLines(i)
End If
Next