Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need Help Opening/Searching File and Inserting a Character

Posted on 2006-11-11
3
Medium Priority
?
199 Views
Last Modified: 2010-04-23
Basically, I'm writing one big program to assist me with a game server I run to combine its ban key records.  The data is stored in a text file as follows:
Profile Name, CD Key Hash, # of Bans, Ban End Date

As players are banned, this banned.txt file is appended.  Unfortunately when you open up this ban file, the records don't space out nicely.  That is, all records are on the same line.  My goal is to get them all on their own line in the above format.

Each ban record will either end in two dashes ("--") or with date and time information in the following format: YYYY-MM-DD HH:NN:SS (n=minutes).  I need to search for these instances and insert an ENTER character, thus putting all records on their own line. That is my question in this post.

I've retrieved the following code off experts-exchange.com, which opens up a text file from the open file dialog box and places its contents into a Rich Text Box.  When I do this, again, my results displayed are jumbled, and are all on the same line.  Inside the TRY block, the code                    RichTextBox1.Text = sr.ReadToEnd() places the text from the text file opened into the rich text box.

EVENTUALLY, I plan on using a menu option to display a new form that will prompt the user to browse for multiple text files, appending each file into one master file and then filtering the results based on the CD Key Hash.  The program will also eventually link to a database and its form will allow the user to navigate through all the ban records, displaying a large multiline text box for the user to write notes on each record.

Again, at this time, I only need help with inserting ENTER characters (or you might call them carriage returns) after certain strings are found.  Please adapt to the following code:

Private FileName As String
Dim sr As StreamReader

    Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click
       
        Dim supportPath As String = "C:\Temp\"

        ofdBanList.Title = "Please select a file to open"
        ofdBanList.InitialDirectory = supportPath


        Try
            With ofdBanList
                .Filter = "Text Files (*.txt)|*.txt"
                If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                    FileName = .FileName
                    sr = New StreamReader(.OpenFile)

                    'Displays the file in a RICH TEXT BOX
                    RichTextBox1.Text = sr.ReadToEnd()
                End If
            End With
        Catch es As Exception
            MessageBox.Show(es.Message)
        Finally
            If Not (sr Is Nothing) Then
                sr.Close()
            End If
        End Try
    End Sub

I'm using VB .NET 2005
0
Comment
Question by:JOSHSKORN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
iHadi earned 1000 total points
ID: 17924020
Hi bb

You can use regular expressions to acheive your goal in one single line. This is a function that breaks up the passed string according to the conditions you mentioned and I've modified you Fileopen method to use it:

    Private Function ToLines(ByVal InputText As String) As String
        Try
            Return System.Text.RegularExpressions.Regex.Replace(InputText, "(--|[\d]{4}-[\d]{2}-[\d]{2}[\s]+[\d]{2}:[\d]{2}:[\d]{2})", "$1" & vbCrLf)
        Catch
            Return ""
        End Try
    End Function

    Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click

        Dim tmpStr As String
        Dim supportPath As String = "C:\Temp\"

        ofdBanList.Title = "Please select a file to open"
        ofdBanList.InitialDirectory = supportPath


        Try
            With ofdBanList
                .Filter = "Text Files (*.txt)|*.txt"
                If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                    FileName = .FileName
                    sr = New StreamReader(.OpenFile)

                    'Displays the file in a RICH TEXT BOX
                    tmpStr = sr.ReadToEnd()
                    RichTextBox1.Text = ToLines(tmpStr)
                End If
            End With
        Catch es As Exception
            MessageBox.Show(es.Message)
        Finally
            If Not (sr Is Nothing) Then
                sr.Close()
            End If
        End Try
    End Sub
0
 
LVL 13

Expert Comment

by:iHadi
ID: 17924023
Sorry JOSHSKORN, I got your name mistaken in the previous post. It should be JOSHSKORN instead of bb
0
 

Author Comment

by:JOSHSKORN
ID: 17924491
Thanks!  Excellent answer.  I encountered a problem that I didn't realize I'd have, but I was able to manipulate the code to produce the desired results.  I changed the following line:

Return System.Text.RegularExpressions.Regex.Replace(InputText, "(ban end date|--|[\d]{4}-[\d]{2}-[\d]{2}[\s]+[\d]{2}:[\d]{2}:[\d]{2})", "$1" & vbCrLf)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question