checking for duplicate lines in text files (vb.net)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
checking for duplicate lines in text files (vb.net)
I currently have an application that prints four text box values to a text file. I was wondering of there was anyway to scan the text file before writing to it, to check to see if that entry already exists. Can anybody help me with this?
Kdr, the description of the programming forums here clearly states that any sort of programming questions are allowed...
Quote:
This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Also note that it is under the Non-*NIX category...
mrobertson, grep is a utility in Linux that is used specifically for what you are trying to do. It looks for text in a file...
For your solution, you'll probably have to write your own routine. It should be pretty simple, though. Just read in the file line by line and compare to what you are looking for...
Hmmm.... that looks like the old VB 6 way of working with files. Is there a reason you're not using the System.IO.StreamReader/StreamWriter objects? I would assume they should be available in VB .Net.
Anyway, in C# an easy way of doing this might be something like:
Code:
public bool TextExistsInFile(string sText, string sFilename)
{
bool bResult = false;
string sLine = null;
System.IO.StreamReader oReader = null;
try
{
oReader = new System.IO.StreamReader(sFilename);
while((sLine = oReader.ReadLine()) != null)
{
// How you do the comparison depends on what you want exactly...
// Exact comparison
if (sLine == sText)
{
bResult = true;
break;
}
// Compare to front
if (sLine.StartsWith(sText))
{
bResult = true;
break;
}
// Exists anywhere in line
if (sLine.IndexOf(sText) >= 0)
{
bResult = true;
break;
}
}
}
catch (Exception e)
{
bResult = false;
}
finally
{
if (oReader != null)
{
oReader.Close();
}
}
return bResult;
}
I underdtand the algorithm, I just do not understand how the sText would be defined. I realize that it is the text on the line that I am reading but how would you define it. Same with sLine?
if (TextExistsInFile("Text you are looking for in file", "Name of file you are looking for text in"))
{
// The text exists in the file
}
else
{
// The text does NOT exist in the file
}
I'm not quite sure how much more clear I can make it...
Originally posted by mrobertson
Ok! I understand what do do now and I got it working great. Is there anyway that could explain my other thread a bit better. I still do not understand how to define the strings that are being compared. I have a string(Parameter 1) that will be going into a text file(Parameter 2) that needs to compared with everystring already in the text file to see if they match.
Code:
if not ExistsInFile(Parameter 1, Parameter 2)
'Append your parameter 1 to the file...
end if
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.