LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   checking for duplicate lines in text files (vb.net) (https://www.linuxquestions.org/questions/programming-9/checking-for-duplicate-lines-in-text-files-vb-net-348852/)

mrobertson 08-01-2005 09:18 AM

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 Kane 08-01-2005 09:32 AM

grep

...

Oh, wait! This is a stupid Windows question on Linux Questions!

mrobertson 08-01-2005 09:46 AM

What does grep mean

deiussum 08-01-2005 10:20 AM

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...

mrobertson 08-01-2005 10:31 AM

would you be able to give me some sample syntax for how to compare?

Here is the code for how I rread the line in:

Code:

FileOpen(10, "C:\Documents and Settings\alogue\Desktop\Coil PDI\PDI.txt", OpenMode.Append)
        PrintLine(10, txtgradecode.Text & "," & txtwidth.Text & "," & txthbgauge.Text & "," & txttargetgauge.Text)
        FileClose(10)


deiussum 08-01-2005 10:43 AM

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;
        }


Kdr Kane 08-01-2005 11:15 AM

I stand corrected.

mrobertson 08-01-2005 11:43 AM

How would this be done in vb.net?

deiussum 08-01-2005 11:57 AM

You can do this using the same objects and methods, just format it to be in VB syntax...

No offense, but this is a pretty simple algorithm. A little reading on the .Net documentation is all you should need to do.

mrobertson 08-01-2005 12:02 PM

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?

deiussum 08-01-2005 12:07 PM

You use that funciton like so:

Code:


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...

deiussum 08-01-2005 12:40 PM

Quote:

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



All times are GMT -5. The time now is 08:31 AM.