Help / example on reading last line from file as well as certain character

tetradelta

Member
Joined
Jan 29, 2013
Messages
8
Programming Experience
5-10
Hi,

I have a text file that looks like the following:

CARTRIDGE1
CARTRIDGE2
CARTRIDGE3
etc..

I need to be able to read the last line of the file as well as grab the number to use for a counter.
So for instance, CATRIDGE3 would be the last line, then I need to add 3+1 to make the next entry 4.

Can someone give me a bit of help on how to grab the last line and isolate the number? Thanks!
 
For example get the last line of File.ReadAllLines and Substring after "CARTRIDGE", convert string to integer and add 1:
Dim number = CInt(IO.File.ReadAllLines("file.txt").Last.Substring(9)) + 1
 
I'm in VS2008, I don't get the option for .Last.Substring
When I type Last at that point I only get LastIndexOf.. am I missing something?

"Last is not a member of System.Array"
 
Last edited:
Without .Last, you should take a slightly longer way:
Dim lines As String() = IO.File.ReadAllLines("file.txt")
Dim lastLine As String = lines(lines.Count - 1)
Dim number = CInt(lastline.Substring(9)) + 1

Also, do have in mind that .Substring(x) will do the job only if you know exactly how long is the name you want to jump over, and the names have all the same length.
Otherwise, you'll need a more complex approach, perhaps using RegularExpressions.
 
I'm in VS2008
That's not what your forum profile says. Anyway this Linq extension is available in VB 2008 default compilation as .Net 3.5.
 
Without .Last, you should take a slightly longer way:
Dim lines As String() = IO.File.ReadAllLines("file.txt")
Dim lastLine As String = lines(lines.Count - 1)
Dim number = CInt(lastline.Substring(9)) + 1

Also, do have in mind that .Substring(x) will do the job only if you know exactly how long is the name you want to jump over, and the names have all the same length.
Otherwise, you'll need a more complex approach, perhaps using RegularExpressions.

Awesome, this seems to work. Although I could not use lines.Count, I had to use lines.Length
 
Last edited:
Well, I wrote a short routine to test your problem, and it doesn't clear the file as you say. This is my code:

    Sub CreateNextItem() Handles Button1.Click
        Dim lines As String() = IO.File.ReadAllLines("file.txt")
        Dim lastLine As String = lines(lines.Count - 1)
        If System.Text.RegularExpressions.Regex.IsMatch(lastLine, "^.+\d+$", System.Text.RegularExpressions.RegexOptions.Singleline) Then
            Dim lastLineDigitStr As String = System.Text.RegularExpressions.Regex.Match(lastLine, "\d+$", System.Text.RegularExpressions.RegexOptions.Singleline).Value
            Dim nextLineDigitStr As String = (Integer.Parse(lastLineDigitStr) + 1).ToString
            Dim nextLine As String = System.Text.RegularExpressions.Regex.Replace(lastLine, "\d+$", nextLineDigitStr, System.Text.RegularExpressions.RegexOptions.Singleline)
            IO.File.AppendAllText("file.txt", vbCrLf & nextLine)
        Else
            MessageBox.Show("Failed: a new item was not created.")
        End If
    End Sub


Be sure you are using the correct methods of IO.File, since there are methods that overwrite file's content (like .WriteAllLines). Should you use these, you must reassemble all the content of the file with the new data and write all of it at once.

Last but not least, I set my IDE to compile this project in .NET 3.5, and yet it has the String().Count property. Perhaps your .NET platform is not updated. If this is not a problem for you, it is strongly advisable that you upgrade, at least, to VS2010/.NET4.0

Good Luck!
 
Thanks VBobCat, it was erasing the file because I was missing the True statement in my declaration to append :)
Dim sw As New System.IO.StreamWriter("C:\adp\cartridgesDONOTDELETE.log", True)
 
Back
Top