3

I have a simple script that deletes the first n lines of a text file.

Const FOR_READING = 1 
Const FOR_WRITING = 2 
strFileName = "C:\scripts\test.txt" 
iNumberOfLinesToDelete = 5 

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 > (iNumberOfLinesToDelete - 1) Then 
      objTS.WriteLine arrLines(i) 
   End If 
Next 

I would like to ask if there is a way if you only have a specific line in the text file which you only want to delete? Meaning based on the line number of the text file.

For example,

1
2
This is line 3
4
5

And you want to remove line number 3. Specifically line number 3.

Result:

1
2
4
5

Is there a way on how to do it?

A big appreciation for the answer and the help.

Djamille
  • 71
  • 1
  • 1
  • 9

2 Answers2

2

Thanks to Ekkehard.Horner for finding my error.

Update:

Const FOR_READING = 1 
Const FOR_WRITING = 2 
strFileName = "C:\scripts\test.txt" 

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 ShouldSkip(i) Then 
      objTS.WriteLine arrLines(i) 
   End If 
Next 

Function ShouldSkip(i)
    Dim arrSkipLines, x
    arrSkipLines = Array(1, 22, 32, 42, 169)
    For Each x In arrSkipLines
        If x = i Then
            ShouldSkip = True
            Exit Function
        End If
    Next

    ShouldSkip = False
End Function
  • Thank you Thomas, I did some changes to get my expected output, instead of using `If UBound(Filter(arrSkipLines, i) = 0 Then` I used `If UBound(Filter(arrSkipLines, i) <> 0 Then`. For the array, i set the array to this: `arrSkipLines = Array(2)` so line number 3 will be removed. Your answer gave me the idea on how to do it, thank you! :) – Djamille Jul 21 '16 at 05:20
  • UBound() returns the last index of an array (i.e. count - 1); so it's -1 for an empty array, or 0 for an array with just one element. – Ekkehard.Horner Jul 21 '16 at 05:51
  • @Ekkehard.Horner damn your right. I'm going to have to modify my answer because Filter is returning all elements containing i. –  Jul 21 '16 at 05:55
  • @Djamille You were right about the <> however I missed an important aspect about use Filter, it returns all elements containing the search phrase. Please use my updated code. –  Jul 21 '16 at 06:05
  • @ThomasInzina - see my temp/pseudo answer. – Ekkehard.Horner Jul 21 '16 at 06:12
1

Just to show @Thomas (and others) that the condition should be

If UBound(Filter(arrSkipLines, i) = -1 Then ' i not found in array/Filter returns empty array


>> WScript.Echo UBound(Filter(Array(1,2,3),2))
>>
0
>> WScript.Echo UBound(Filter(Array(1,2,3),4))
>>
-1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96