Hi All,

I am trying to write a small app that reads a logfile in the form of:

01/07/2013 07:40:12 => Start of process.
01/07/2013 07:40:17 => Preparing authentication token
01/07/2013 07:40:17 => Attempting authentication with supplied credentials
01/07/2013 07:40:20 => Authentication successful!
01/07/2013 07:40:20 => Using BinarySecurityToken: a9d4e88c077e342d

01/07/2013 07:40:20 => Checking incoming directory for XML files to process.
01/07/2013 07:40:20 => Converting XML File: D:\Lagan\Outfiles\yhn_ng_lag_upd_20130701.xml
01/07/2013 07:40:21 => Conversion successful.

01/07/2013 07:40:21 => Record Number 1
01/07/2013 07:40:21 => Now processing ObjectID: 101006826548
01/07/2013 07:40:23 => No valid e-mail address specified
01/07/2013 07:40:23 => No valid e-mail address specified
01/07/2013 07:40:23 => Attempting update individual via web service
01/07/2013 07:40:26 => 101006826548 Updated successfully!
01/07/2013 07:40:26 => Completed processing ObjectID: 101006826548

01/07/2013 07:40:26 => Record Number 2
01/07/2013 07:40:26 => Now processing ObjectID: 101006826560
01/07/2013 07:40:29 => Postal address has Update action but no ID
01/07/2013 07:40:29 => No valid e-mail address specified
01/07/2013 07:40:29 => No valid e-mail address specified
01/07/2013 07:40:29 => Attempting update individual via web service
01/07/2013 07:40:31 => 101006826560 Updated successfully!
01/07/2013 07:40:31 => Completed processing ObjectID: 101006826560

01/07/2013 07:40:31 => Record Number 3
01/07/2013 07:40:31 => Now processing ObjectID: 101006826565
01/07/2013 07:40:34 => Postal address has Update action but no ID
01/07/2013 07:40:34 => No valid e-mail address specified
01/07/2013 07:40:34 => No valid e-mail address specified
01/07/2013 07:40:34 => Attempting update individual via web service
01/07/2013 07:40:36 => 101006826565 Updated successfully!
01/07/2013 07:40:36 => Completed processing ObjectID: 101006826565
.
.
.

I am searching on a string selected from a combo box e.g. 'Postal address has Update action but no ID' and then I am tring to get the last line of the block with the ObjectID to go to a textbox.

I can currently see the number of occurences in the combo box when I write back to it but cannot get the line I need with the ID.

My code so far is:

 private void butSearch_Click(object sender, EventArgs e)
        {
          string line = Environment.NewLine;
            if (comboSearch.Text == string.Empty)
            {
                MessageBox.Show("Please add a search string", "Search String Missing",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                string stringPattern = comboSearch.SelectedItem.ToString();

                OpenFileDialog openFileDialog1 = new OpenFileDialog();

                openFileDialog1.InitialDirectory = @Properties.Settings.Default.Path;
                openFileDialog1.Title = "Browse Text Files";
                openFileDialog1.CheckFileExists = true;
                openFileDialog1.CheckPathExists = true;
                openFileDialog1.DefaultExt = "txt";
                openFileDialog1.Filter = Properties.Settings.Default.Filter;
                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;
                openFileDialog1.ReadOnlyChecked = true;
                openFileDialog1.ShowReadOnly = true;

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string fileContents;
                    string fileName = openFileDialog1.FileName.ToString();
                    int stringCount = 0;

                    StreamReader searchFile = new StreamReader(@fileName);

                    while (!searchFile.EndOfStream)
                    {
                        fileContents = searchFile.ReadLine();

                        if (fileContents.Contains(stringPattern))
                        {
                            stringCount = stringCount + 1;

                            if (((String)comboSearch.SelectedItem) == "Postal address has Update action but no ID")
                            {   
                                string data = "Completed processing ObjectID: " + line;
                                data.Contains("Completed processing ObjectID");
                                textResults.Text = data.ToString();
                                string result = data.Substring(0, data.IndexOf("\r\n"));
                                //LogMessageToFile(result);
                            }            
                        }
                    }

                    comboSearch.Text = "Number of occurrences = " + stringCount;

                    searchFile.Close();
                }

                if (((String)comboSearch.SelectedItem) == "Postal address has Update action but no ID")
                {
                    butReport.Enabled = true;
                }
            }
        }

Many thanks in advance.

Here's a simnple routine that will fill your textbox with the ObjectId's. Pass the 2 search patterns in the order you want them searched and the filename, and the texztbox will take the id's. Use textResults.Lines.Count() to get the number of items after the sub routine is finished.

    private void GetData(string pattern1, string pattern2, string filename)
    {
        StreamReader searchFile = new StreamReader(@filename);
        //flag to indicate first pattern found
        bool found = false;
        while (!searchFile.EndOfStream)
        {
            string data = searchFile.ReadLine();
            //check for pattern1
            if (data.Contains(pattern1))
            {
                found = true;
            }
            else
            {
                //if the flag is set then check for pattern2
                if (found && data.Contains(pattern2))
                {
                    //upon success append the substring we want to the textbox
                    textResults.AppendText(data.Substring(data.LastIndexOf(' ')) + "\n");
                    found = false;
                }
            }
        }
    }

Hi tinstaafl,

Little bit of tweeking and it works brilliantly thanks.

Really appreciate it.

Ken

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.