Remove lines from a file: Difference between revisions

Line 2,636:
 
=={{header|VBA}}==
First way : read the text file line by line
<lang vb>Option Explicit
 
Line 2,696 ⟶ 2,697:
9
10</pre>
 
Second Way : Read the text file at once and work with Arrays
<lang vb>Option Explicit
 
Sub Main()
'See Output First call
OtherWay "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 11, 5
'See Output Second call
OtherWay "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 8, 5
'See Output Third call
OtherWay "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 3, 5
End Sub
 
Private Sub OtherWay(StrFile As String, StartLine As Long, NumberOfLines As Long)
Dim Nb As Integer, s As String, arr, i As Long, out() As String, j As Long
Nb = FreeFile
Open StrFile For Input As #Nb
s = Input(LOF(1), #Nb)
Close #Nb
arr = Split(s, Chr(13))
If StartLine >= UBound(arr) + 1 Then
MsgBox "First call : " & vbCrLf & " The file contains only " & UBound(arr) + 1 & " lines"
ElseIf StartLine + NumberOfLines > UBound(arr) + 1 Then
MsgBox "Second call : " & vbCrLf & " You only can remove " & UBound(arr) + 1 - StartLine & " lines"
Else
For i = LBound(arr) To UBound(arr)
If i < StartLine - 1 Or i >= StartLine + NumberOfLines - 1 Then
ReDim Preserve out(j)
out(j) = arr(i)
j = j + 1
End If
Next i
Nb = FreeFile
Open StrFile For Output As #Nb
Print #Nb, Join(out, Chr(13))
Close #Nb
End If
End Sub</lang>
 
{{in}}
<pre>1
2
3
4
5
6
7
8
9
10</pre>
 
{{out}}
<pre>First call :
The file contains only 10 lines
Second call :
You only can remove 2 lines
Third call :
1
2
8
9
10
</pre>
 
=={{header|VBScript}}==
Anonymous user