-
February 25th, 2014, 12:37 AM
#1
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
-
February 25th, 2014, 01:10 AM
#2
Re: Use StreamReader to find text within file
Try this:
Code:
MessageBox.Show(Line.ToString())
-
February 25th, 2014, 09:58 AM
#3
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
-
February 25th, 2014, 12:39 PM
#4
Re: Use StreamReader to find text within file
Doesn't it appear within that line in the messagebox?
-
February 25th, 2014, 03:04 PM
#5
Re: Use StreamReader to find text within file
Originally Posted by dglienna
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
-
February 25th, 2014, 04:28 PM
#6
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));
}
}
-
February 25th, 2014, 11:08 PM
#7
Re: Use StreamReader to find text within file
or, pipe it to a text file. > outputfile.txt
-
February 28th, 2014, 10:42 AM
#8
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
-
February 28th, 2014, 12:24 PM
#9
Re: Use StreamReader to find text within file
You can use string functions for that line
-
February 28th, 2014, 06:04 PM
#10
Re: Use StreamReader to find text within file
Originally Posted by netman06
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.
-
March 1st, 2014, 12:09 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|