如果行具有值,则从文本文件中删除前面的行
Delete preceding lines from text file if line has value
我有一个包含数字的文本文件,我必须删除给定数字之前的行。
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
试试这个删除行数,然后自己试试其他的。
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
我有一个包含数字的文本文件,我必须删除给定数字之前的行。
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
试试这个删除行数,然后自己试试其他的。
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