Task

Remove a specific line or a number of lines from a file.

This should be implemented as a routine that takes three parameters (filename, starting line, and the number of lines to be removed).

For the purpose of this task, line numbers and the number of lines start at one, so to remove the first two lines from the file foobar.txt, the parameters should be: foobar.txt, 1, 2

Empty lines are considered and should still be counted, and if the specified line is empty, it should still be removed.

An appropriate message should appear if an attempt is made to remove lines beyond the end of the file.




sNewFile As String                                                                  'Global string for the 'New file' details
 
Public Sub Main()
Dim sFileName As String = User.Home &/ "foobar.txt"                                 'File name
 
sNewFile = DeleteLines(sFileName, 1, 2)                                             'Send the details to the DeleteLine routine 'The parameters should be: foobar.txt, 1, 2'
Print "New file: -" & gb.NewLine & sNewFile                                         'Print details of the changed file
File.Save(sFileName, sNewFile)                                                      'Save the file with the original name
 
End
 
Public Sub DeleteLines(sFile As String, siStart As Short, siNum As Short) As String 'DeleteLines routine
Dim sData As New String[]                                                           'To store the existing file data
Dim siCount As Short                                                                'Counter
Dim sTemp, sDel As String                                                           'String variables
 
For Each sTemp In Split(File.Load(sFile), gb.NewLine)                               'Load the file, split the lines by NewLine 
  sData.Add(sTemp)                                                                  'Add to sData
Next
 
Print "Original file: -"                                                            'Print Title
 
If siStart + siNum > sData.max Then                                                 'Check an attempt is made to remove lines beyond the end of the file
  Print "Not enough lines in file to carry out request"                             'An appropriate message should appear if so
  Return ""                                                                         'Return nothing
Endif
 
Dec siStart                                                                         'For the purpose of this task, line numbers start at one (Program starts at 0)
 
For siCount = siStart To (siStart + siNum) - 1                                      'Loop through the lines to be deleted 
  sDel &= Str(siCount) & " "                                                        'Add then to sDel
Next
 
siCount = -1                                                                        'Reset counter
 
For Each sTemp In sData                                                             'For each line in the file..
  Inc siCount                                                                       'Increase counter
  Print sTemp                                                                       'Print the line
  If InStr(sDel, Str(siCount) & " ") Then Continue                                  'If the line number is listed in sDel then jump to the end of the loop
  sNewFile &= sTemp & gb.NewLine                                                    'Add the lines not to be removed into sNewFile
Next
 
Return sNewFile                                                                     'Return the details
 
End