-
Apr 12th, 2012, 11:39 AM
#1
Thread Starter
Lively Member
Getting specified text from text file
I whould like to get some names from a text file inside of it.
For example my text file looks like this :
Code:
SERVER: New client joined this server...
>>> Player "NorbyTKD" is connecting...
>>> Player "NorbyTKD" joined the match.
>>> Player "NorbyTKD" has entered the game.
SERVER: Client closed by server...
>>> Player "NorbyTKD" disconnected.
SERVER: Client closed by server...
>>> Player "Morten Degn" disconnected.
SERVER: Client closed by server...
>>> Player "John" disconnected.
I need the line >>> Player "NorbyTKD" joined the match. only the player name
in this case : NorbyTKD listed in textbox and when this user is disconnected like in this line :>>> Player "NorbyTKD" disconnected. it must read this also and delete the disconnected user from textbox.
text box can have over 1000 lines depence on server runtime.
Thanks
-
Apr 12th, 2012, 11:58 AM
#2
Re: Getting specified text from text file
VB Code:
using (StreamReader sr = new StreamReader("file.txt")) { while (sr.Peek() > -1) { string ln = sr.ReadLine(); if (ln.Contains(@"""")) { string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase) select m.Value).ToArray()[0]; textBox1.Text += values + Environment.NewLine; } } }
This will return to you all the names from that file.
Last edited by AceInfinity; Apr 12th, 2012 at 12:04 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 12:03 PM
#3
Thread Starter
Lively Member
Re: Getting specified text from text file
Originally Posted by AceInfinity
VB Code:
String = @">>> Player ""Morten Degn"" disconnected.""";
string[] values = (from Match m in Regex.Matches(InputString, @"""(.*?)""", RegexOptions.IgnoreCase)
select m.Value).ToArray();
textBox1.Text = String.Join(",", values);
This will return to you "Morten Degn", just as an example
,,
if I put your code to a button I have 5 errors. also it must read first the test.txt and get the names from there. the disconnected and connected names are all random names . that we can not know.
-
Apr 12th, 2012, 12:04 PM
#4
Re: Getting specified text from text file
I edited my post, now try. You should attempt this yourself you know. Reading from a file there's several methods you can use, i've shown you one now. That first code I posted was more of an example, and was not meant to be copied out directly into your IDE for it to automatically work.
Cheers
~Ace
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 12:06 PM
#5
Thread Starter
Lively Member
Re: Getting specified text from text file
Originally Posted by AceInfinity
I edited my post, now try. You should attempt this yourself you know. Reading from a file there's several methods you can use, i've shown you one now.
Cheers
errors
Code:
Error 1 The name 'Regex' does not exist in the current context
Error 2 The name 'RegexOptions' does not exist in the current context
Error 3 The type or namespace name 'Match' could not be found (are you missing a using directive or an assembly reference?)
-
Apr 12th, 2012, 12:17 PM
#6
Re: Getting specified text from text file
Originally Posted by deve
errors
Code:
Error 1 The name 'Regex' does not exist in the current context
Error 2 The name 'RegexOptions' does not exist in the current context
Error 3 The type or namespace name 'Match' could not be found (are you missing a using directive or an assembly reference?)
You're looking just to copy out the code that I provide, it doesn't exist becauase you havent declared that you're using Regex from the namespace it resides in.
http://msdn.microsoft.com/en-us/libr...ons.regex.aspx
It won't help you if you don't attempt to take the initiative to problem solve and learn individually.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 12:24 PM
#7
Thread Starter
Lively Member
Re: Getting specified text from text file
sorry I did now , I was just following you
using System.Text.RegularExpressions;
but there is a smal lproblem it list everything that has " " like there is things like
LOG file "Game.LOG" created in 04/12/12 14:15:38 it does show now the Game.LOG also , just the connected players I wont and also needs to check if user is disconnected not list it.
-
Apr 12th, 2012, 12:31 PM
#8
Re: Getting specified text from text file
using System.Text.RegularExpressions;
Yes!! Good job
"Game.LOG" did not exist in the sample you gave me however, so I minimized it to check for quotes only
To remove them you can use the Replace Function, or maybe even the SubString Method if you wanted.. I'll see how this can be implemented and provide an example.
EDIT: I see however 3 things...
1. is connecting...
2. joined the match.
3. has entered the game.
Do you want me to display just [Connected] instead of these entries or keep this wording? And it would be very helpful if you could provide me with more format to deal with, as i've mentioned: Game.LOG doesn't exist in the current snippet of log file that you have posted to me so far.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 12:33 PM
#9
Thread Starter
Lively Member
Re: Getting specified text from text file
thanks but I did write on my first post that it needs to delete it from the list also if user disconnected.
-
Apr 12th, 2012, 12:37 PM
#10
Re: Getting specified text from text file
Alright but i'm still confused, as mentioned there are more than one indicator of a [Connection]
1. is connecting...
2. joined the match.
3. has entered the game.
Do you want me to keep this wording, or find these lines and replace them all with [Connected] or [Connecting] or something?
Example:
- John A [Connecting]
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 12:39 PM
#11
Thread Starter
Lively Member
Re: Getting specified text from text file
this line is important : 2. joined the match. We need the user on this line without " " in to textbox and when this user have a disconnect it must delete from the list.
-
Apr 12th, 2012, 12:45 PM
#12
Re: Getting specified text from text file
Why not use a ListView or ListBox in this case? And if they are disconnected, and that user gets removed, then shouldn't all of the existing entries of users be listed as "connected" anyways? Therefore I don't see a reason to provide that bit of information in the control.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 12:49 PM
#13
Thread Starter
Lively Member
Re: Getting specified text from text file
I can use Listview no problem anything that does the trick. so far I have changed your code like this :
if (ln.Contains(@""" joined the match."))
it just shows connected people alltime and not those people who are connected online. Thats why I need it to check also if it is disconnected deleted from the lsit so I have only online users.
-
Apr 12th, 2012, 12:51 PM
#14
Re: Getting specified text from text file
Also I have more questions as this is getting really blurry for me. If this is for a game (seemingly), and you want to remove these names when they say they are disconnected, would this not mean that you would have to read this file on an interval to read the last line to determine whether to add or remove an entry from the data? I can't see this file being updated directly, and instead, being appended to, meaning the last line is the only important piece of information here. And to have to read it at an interval, you may miss some things if you don't read it fast enough, as if 10 people join in a second. How are you supposed to know where you last left off reading the file and closing it to determine where to being reading to update the data?
Your edit makes sense, in the beginning I had no idea what you wanted though exactly, so this is what I began doing:
vb Code:
using (StreamReader sr = new StreamReader("file.txt")) { while (sr.Peek() > -1) { string ln = sr.ReadLine(); if (ln.Contains("Player")) { string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase) select m.Value).ToArray()[0]; string status = ln.Contains("disconnected") ? " [Disconnected]" : " [Connected]"; listBox1.Items.Add(values.Replace((char)34, '\n') + status + Environment.NewLine); } } }
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 12:57 PM
#15
Thread Starter
Lively Member
Re: Getting specified text from text file
it has System.IndexOutOfRangeException when debug
-
Apr 12th, 2012, 01:00 PM
#16
Re: Getting specified text from text file
Ok, from the sample log you gave me though it works, so it must be because of some entry in the log you're analyzing. I can't help you with that until I get a larger sample log...
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 01:03 PM
#17
Thread Starter
Lively Member
Re: Getting specified text from text file
Originally Posted by AceInfinity
Ok, from the sample log you gave me though it works, so it must be because of some entry in the log you're analyzing. I can't help you with that until I get a larger sample log...
Code:
-------------------------------------------------------------------------
G A M E R E S U L T S
-------------------------------------------------------------------------
Location : Utah
Season : Pre-Rut
Time : 04/12/12 14:32:49
Duration : 15 minutes
Game type : Best_Trophy
Difficulty: Expert
Result : Game_has_a_winner!
Players : 2
1. "Charles Bronson" is a Winner!
Trophy(ies) : 14
Score : 393.331
Best Trophy : Male Whitetail
Points : Total (393.331) = Animal score (168.023)
Distance : 86.29 meters
2. "YONAH"
-------------------------------------------------------------------------
SERVER: Client closed by server...
>>> Player "YONAH" disconnected.
SERVER: Client closed by server...
>>> Player "Charles Bronson" disconnected.
>>> Game started.
-------------------------------------------------------------------------
G A M E R E S U L T S
-------------------------------------------------------------------------
Location : Utah
Season : Pre-Rut
Time : 04/12/12 14:48:24
Duration : 15 minutes
Game type : Best_Trophy
Difficulty: Expert
Result : Time_limit_hit.
Players : 0
-------------------------------------------------------------------------
SERVER: New client joined this server...
>>> Player "Daniel" is connecting...
>>> Player "Daniel" joined the match.
>>> Game started.
>>> Player "Daniel" has entered the game.
SERVER: Client closed by server...
>>> Player "Daniel" disconnected.
SERVER: New client joined this server...
>>> Player "Kaan" is connecting...
>>> Player "Kaan" joined the match.
>>> Player "Kaan" has entered the game.
SERVER: Client closed by server...
>>> Player "Kaan" disconnected.
-------------------------------------------------------------------------
G A M E R E S U L T S
-------------------------------------------------------------------------
Location : Utah
Season : Pre-Rut
Time : 04/12/12 15:22:23
Duration : 15 minutes
Game type : Best_Trophy
Difficulty: Expert
Result : Time_limit_hit.
Players : 0
-------------------------------------------------------------------------
SERVER: New client joined this server...
>>> Player "jeff" is connecting...
>>> Player "jeff" joined the match.
>>> Game started.
[ERROR] *** Hunter initialization was rejected from player "jeff"!!!
>>> Player "jeff" disconnected.
SERVER: Client closed by server...
-------------------------------------------------------------------------
G A M E R E S U L T S
-------------------------------------------------------------------------
Location : Utah
Season : Pre-Rut
Time : 04/12/12 15:50:09
Duration : 15 minutes
Game type : Best_Trophy
Difficulty: Expert
Result : Time_limit_hit.
Players : 0
-------------------------------------------------------------------------
log file goes like this.
-
Apr 12th, 2012, 01:25 PM
#18
Re: Getting specified text from text file
That error comes from a line like this then:
As it doesn't contain any quotes.
Modify the check line:
Code:
if (ln.Contains("Player"))
Code:
if (ln.Contains("Player") && ln.Contains((char)34))
something like that... Or your line would work, initially this is what I would have done as I didn't know you only wanted to retrieve the lines that state when they entered a match.
Code:
if (ln.Contains(@""" joined the match."))
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 01:30 PM
#19
Thread Starter
Lively Member
Re: Getting specified text from text file
changed to : if (ln.Contains(">>> Player")) works good now thanks. But then a connection has 3 parts like :
1. is connecting...
2. joined the match.
3. has entered the game.
we need to store only 2.
this way it list s only the connected people : if (ln.Contains(" joined the match."))
-
Apr 12th, 2012, 01:41 PM
#20
Thread Starter
Lively Member
Re: Getting specified text from text file
I can see dublicates also , same name twice when user was disconnected and connected back for example. it doesnt check for dublicates and list only online users.
-
Apr 12th, 2012, 01:54 PM
#21
Re: Getting specified text from text file
Originally Posted by deve
I can see dublicates also , same name twice when user was disconnected and connected back for example. it doesnt check for dublicates and list only online users.
Because you're appending to the text, you need to clear it out before updating it. Also, it changes to [Connected] if it finds one of these 3
1. is connecting...
2. joined the match.
3. has entered the game.
So if it finds all 3, then there will be duplicates, if you only check for "joined the match." and clear the text field whenever you update it, you won't see duplicates, otherwise if this file does have duplicates, then you could create an array and get distinct items or use LINQ.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 01:56 PM
#22
Thread Starter
Lively Member
Re: Getting specified text from text file
well I have it on a button click for now and the listbox1 is empty when Ipress the button I get some dublicates and some people who are not online.
-
Apr 12th, 2012, 02:02 PM
#23
Thread Starter
Lively Member
Re: Getting specified text from text file
between Iuse your code like this :
Code:
using (StreamReader sr = new StreamReader(textBox1.Text + @"\Game\Game.LOG"))
{
while (sr.Peek() > -1)
{
string ln = sr.ReadLine();
if (ln.Contains(" joined the match."))
{
string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
select m.Value).ToArray()[0];
//string status = ln.Contains("disco") ? "" : "";
listBox1.Items.Add(values.Replace((char)34, '\n') + Environment.NewLine);
}
}
}
-
Apr 12th, 2012, 02:14 PM
#24
Re: Getting specified text from text file
Code:
listBox1.Items.Clear();
At the top above "using".
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 02:16 PM
#25
Thread Starter
Lively Member
Re: Getting specified text from text file
Originally Posted by AceInfinity
Code:
listBox1.Items.Clear();
At the top above "using".
now there are 3 people connected and it shows on our List 20 and some of them dublicated
-
Apr 12th, 2012, 02:34 PM
#26
Re: Getting specified text from text file
Can you provide some screenshots of what you mean? I have no idea honestly for what you're talking about then. And you are using the code in this post right? http://www.vbforums.com/showpost.php...8&postcount=23
Use this bit of code to remove duplicates:
Code:
string[] arr = textBox1.Lines;
textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());
Last edited by AceInfinity; Apr 12th, 2012 at 02:38 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 02:39 PM
#27
Thread Starter
Lively Member
Re: Getting specified text from text file
-
Apr 12th, 2012, 02:43 PM
#28
Re: Getting specified text from text file
Try this:
vb Code:
listBox1.Items.Clear(); using (StreamReader sr = new StreamReader(textBox1.Text + @"\Game\Game.LOG")) { while (sr.Peek() > -1) { string ln = sr.ReadLine(); if (ln.Contains(" joined the match.")) { string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase) select m.Value).ToArray()[0]; //string status = ln.Contains("disco") ? "" : ""; textBox1.Text += values.Replace((char)34, '\n') + Environment.NewLine; } } } string[] arr = textBox1.Lines; textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());
This will remove all the duplicates. Assuming that your display "2/16" is correct. I'm not sure how your validating that.
Last edited by AceInfinity; Apr 12th, 2012 at 03:20 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 02:48 PM
#29
Thread Starter
Lively Member
Re: Getting specified text from text file
Nope I am using like this :
Code:
listBox1.Items.Clear();
using (StreamReader sr = new StreamReader(textBox1.Text + @"\Game\Game.LOG"))
{
while (sr.Peek() > -1)
{
string ln = sr.ReadLine();
string[] arr = textBox1.Lines;
textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());
if (ln.Contains(" joined the match."))
{
string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
select m.Value).ToArray()[0];
//string status = ln.Contains("disconnected") ? " [Disconnected]" : " [Connected]";
listBox1.Items.Add(values.Replace((char)34, '\n') + Environment.NewLine);
}
is it correct to type just TextBox1.Text = coz the full url is like : textBox1.Text + @"\Game\Game.LOG" from the log file we read.
-
Apr 12th, 2012, 03:04 PM
#30
Re: Getting specified text from text file
Originally Posted by deve
Nope I am using like this :
Code:
listBox1.Items.Clear();
using (StreamReader sr = new StreamReader(textBox1.Text + @"\Game\Game.LOG"))
{
while (sr.Peek() > -1)
{
string ln = sr.ReadLine();
string[] arr = textBox1.Lines;
textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());
if (ln.Contains(" joined the match."))
{
string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
select m.Value).ToArray()[0];
//string status = ln.Contains("disconnected") ? " [Disconnected]" : " [Connected]";
listBox1.Items.Add(values.Replace((char)34, '\n') + Environment.NewLine);
}
is it correct to type just TextBox1.Text = coz the full url is like : textBox1.Text + @"\Game\Game.LOG" from the log file we read.
Yes it's fine, you may want to validate that location though.
You're forgetting my Distinct function however.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 03:06 PM
#31
Thread Starter
Lively Member
Re: Getting specified text from text file
well I can put it outside of the loop but same efect have the dublicates and the offline users some of them listed still.
-
Apr 12th, 2012, 03:15 PM
#32
Re: Getting specified text from text file
Originally Posted by deve
well I can put it outside of the loop but same efect have the dublicates and the offline users some of them listed still.
From the code you posted you weren't using the function to return distinct values that I posted. And make sure that these entries don't have any spaces around them, you could use the Trim() method for that.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 03:17 PM
#33
Thread Starter
Lively Member
Re: Getting specified text from text file
well thats how my code looks now :
Code:
listBox1.Items.Clear();
using (StreamReader sr = new StreamReader(textBox1.Text + @"\Game\Game.LOG"))
{
while (sr.Peek() > -1)
{
string ln = sr.ReadLine();
if (ln.Contains(" joined the match."))
{
string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
select m.Value).ToArray()[0];
//string status = ln.Contains("disconnected") ? " [Disconnected]" : " [Connected]";
listBox1.Items.Add(values.Replace((char)34, '\n') + Environment.NewLine);
string[] arr = textBox1.Lines;
textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());
}
-
Apr 12th, 2012, 03:18 PM
#34
Re: Getting specified text from text file
It's working for me on the full log... So i'm not sure what you're doing wrong, but the code works.
Edit: Do not put this in the loop though, keep it outside of the loop:
Code:
string[] arr = textBox1.Lines;
textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());
Note: You're adding items to a listview and removing duplicates from the textbox...
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 03:26 PM
#35
Re: Getting specified text from text file
If you're using a listbox now and decided to go that way:
vb Code:
listBox1.Items.Clear(); List<string> data = new List<string>(); using (StreamReader sr = new StreamReader("File.txt")) { while (sr.Peek() > -1) { string ln = sr.ReadLine(); if (ln.Contains(" joined the match.")) { string val = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase) select m.Value).ToArray()[0]; data.Add(val.Replace((char)34, ' ').Trim()); } } } listBox1.Items.AddRange(data.Distinct().ToArray());
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 03:30 PM
#36
Thread Starter
Lively Member
Re: Getting specified text from text file
yes now it works it shows all the connected user from the start of log until the end but we do not have a check if user disconnected delete it from the list.
-
Apr 12th, 2012, 03:34 PM
#37
Re: Getting specified text from text file
That's all managable with the List<string> that I created.
Look here: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
Example:
Code:
data.Remove("item");
Where "item" is the data value in the list, then clear and update the listbox with this managed list. You should be able to do the rest now
The code I posted relies on that text file, so whenever you update your listbox with THAT code, it will put whatever it parses from the textfile. So there is no check if they are disconnected, it only checks online.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Apr 12th, 2012, 03:36 PM
#38
Thread Starter
Lively Member
Re: Getting specified text from text file
ok thank you very much +++
-
Apr 13th, 2012, 07:11 AM
#39
Thread Starter
Lively Member
Re: Getting specified text from text file
well I chould not figure out on how to delete the disconnected users from the link you gave me. My english isnt that good ...
-
Apr 13th, 2012, 09:35 PM
#40
Re: Getting specified text from text file
As I mentioned, this file gets appended to everytime someone joins, so instead of it containing ONLY the data that you want to parse into the control on the form, it may contain data that used to be useful but is no longer useful because it was "old news" from statistics of a person joining, and the stuff at the end of the file is the "new news" (new data) that we actually want inside of the control.
So there's no way to update this "view" accurately by reading from that file...
You'll have to probably try and intercept the incoming data as it gets written to the file or try something else.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
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
|