CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2012
    Posts
    36

    Use StreamReader to find text within file

    Hello,

    I'm trying to figure out how to search a text file looking for a word "VMware Tools", then read that line and extract only this specific part.
    {AADE8E70-A924-4EDF-BD94-08A31758A531}

    From this line:

    VMware Tools {AADE8E70-A924-4EDF-BD94-08A31758A531} 20140214

    Code:
    int counter = 0;
                string line;
    
                System.IO.StreamReader file = new System.IO.StreamReader("C:\\temp\\list.txt");
                List<string> lines = new List<string>();
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("VMware Tools"))
                    {
    
                        //MessageBox.Show("found It");
                            
    
                        int x = counter - 1;
                        //Console.WriteLine(x); // this will write the line number not its contents
    
                        MessageBox.Show(x.ToString());
                    }
    
                    counter++;
                }
    
                file.Close();
    I can get which line it is found on, but do not know how to code it to extract it into a variable.

    Any help would be great.

    Thanks,

    -Mike

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Use StreamReader to find text within file

    Try this:

    Code:
    MessageBox.Show(Line.ToString())
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    May 2012
    Posts
    36

    Re: Use StreamReader to find text within file

    Hi dglienna,

    I have it working with the way the code is now. But I need to find and/or search within the text file, looking for just "VM Tools", once found, next find numbers contained with the {}, like this {AADE8E70-A924-4EDF-BD94-08A31758A531}, then store this in a string or value.

    Thanks,

    -Mike

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Use StreamReader to find text within file

    Doesn't it appear within that line in the messagebox?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    May 2012
    Posts
    36

    Re: Use StreamReader to find text within file

    Quote Originally Posted by dglienna View Post
    Doesn't it appear within that line in the messagebox?
    Yes, but I need to figure out how to just pull out the IdentifyingNumber , which will always be a number within {}

    It might be helpful of you run this command on your computer to see the output.

    wmic product list


    Now open this file in Notepad++ to get the correct formatting correctly.
    Here is a sample output line:

    Description IdentifyingNumber InstallDate InstallLocation InstallState Name PackageCache SKUNumber Vendor Version
    Microsoft Application Error Reporting {95120000-00B9-0409-0000-0000000FF1CE} 20140110 5 Microsoft Application Error Reporting C:\WINDOWS\Installer\9283634.msi Microsoft Corporation

    So, if you use this as an example, say we need to find Microsoft Application Error Reporting and then read the number with th {}, which is {95120000-00B9-0409-0000-0000000FF1CE}, now store this in a value.

    To recap and make it easier to understand.

    Step # 1

    Search for Microsoft Application Error Reporting first section of file.

    Step # 2

    Search that line for {95120000-00B9-0409-0000-0000000FF1CE}

    Step # 3

    Store this in a string or value.

    Thanks again for any and all help on this code.

    -Mike

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Use StreamReader to find text within file

    Try something like this. Double check the indexes to make sure there aren't any off by 1 errors.
    Code:
    if (line.Contains("Microsoft Application Error Reporting"))
    {
      var startIndex = line.IndexOf("{");
      var endIndex = line.IndexOf("}");
    
      if(startIndex != -1 && endIndex != -1)
      {
        var guid = new Guid(line.SubString(startIndex, endIndex - startIndex)); 
      }                     
    }

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Use StreamReader to find text within file

    or, pipe it to a text file. > outputfile.txt
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    May 2012
    Posts
    36

    Re: Use StreamReader to find text within file

    Hi Arjay,

    I tested the code and I'm getting this error message.

    FormatException was unhandled

    Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

    I look at a few MSDN articles, but did not find anything that I understand, to help myself with this error message.

    Code:
    private void button18_Click(object sender, EventArgs e)
            {
                int counter = 0;
                string line;
    
                System.IO.StreamReader file = new System.IO.StreamReader("C:\\temp\\list.txt");
                List<string> lines = new List<string>();
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("VMware Tools"))
                    {
                        var startIndex = line.IndexOf("{");
                        var endIndex = line.IndexOf("}");
    
                        if (startIndex != -1 && endIndex != -1)
                        {
                            var guid = new Guid(line.Substring(startIndex, endIndex - startIndex));
    
                            counter++;
    
                            MessageBox.Show(line);
                        }
                        file.Close();
                        }
                }
    Here is the contents of string line, it does have what I need in it.

    line "VMware Tools {AADE8E70-A924-4EDF-BD94-08A31758A531} 20140214

    Raw output here.

    line "VMware Tools {AADE8E70-A924-4EDF-BD94-08A31758A531} 20140214 C:\\Program Files\\VMware\\VMware Tools\\ 5 VMware Tools C:\\Windows\\Installer\\23367.msi VMware, Inc. 9.2.0.15626 " string


    Thanks,

    -Mike

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Use StreamReader to find text within file

    You can use string functions for that line
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Use StreamReader to find text within file

    Quote Originally Posted by netman06 View Post
    Hi Arjay,

    I tested the code and I'm getting this error message.

    FormatException was unhandled

    Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

    I look at a few MSDN articles, but did not find anything that I understand, to help myself with this error message.

    Code:
    private void button18_Click(object sender, EventArgs e)
            {
                int counter = 0;
                string line;
    
                System.IO.StreamReader file = new System.IO.StreamReader("C:\\temp\\list.txt");
                List<string> lines = new List<string>();
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("VMware Tools"))
                    {
                        var startIndex = line.IndexOf("{");
                        var endIndex = line.IndexOf("}");
    
                        if (startIndex != -1 && endIndex != -1)
                        {
                            var guid = new Guid(line.Substring(startIndex, endIndex - startIndex));
    
                            counter++;
    
                            MessageBox.Show(line);
                        }
                        file.Close();
                        }
                }
    Here is the contents of string line, it does have what I need in it.

    line "VMware Tools {AADE8E70-A924-4EDF-BD94-08A31758A531} 20140214

    Raw output here.

    line "VMware Tools {AADE8E70-A924-4EDF-BD94-08A31758A531} 20140214 C:\\Program Files\\VMware\\VMware Tools\\ 5 VMware Tools C:\\Windows\\Installer\\23367.msi VMware, Inc. 9.2.0.15626 " string


    Thanks,

    -Mike
    All that is wrong is the SubString indexes are wrong on the new Guid(...) line. That's why I mentioned the following warning before my code snippet: "Double check the indexes to make sure there aren't any off by 1 errors."

    Just change the code slightly from:
    Code:
    var guid = new Guid(line.Substring(startIndex, endIndex - startIndex));
    to
    Code:
    var guidString = line.Substring(startIndex, endIndex - startIndex);
    var guid = new Guid(guidString);
    then put a breakpoint on the guid string and verify it's what you need.

  11. #11
    Join Date
    May 2012
    Posts
    36

    Re: Use StreamReader to find text within file

    Thanks, Arjay,

    Got it working great, now I understand the index's, had to adjust it a few ticks and thanks to you I learn something new, using C#.

    -Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured