Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Hello I have a search question. This is my problem... I have a form with different txtboxes. I type in an id number in the ID number txtbox and hit find. It looks for a txt file named (txtBoxID.text + ".txt"). what I want to do is... If I am not sure of the id number, I want for example to type in the last name of the individual, the textbox that is for the last name... and have it look through all the .txt files in the folder for that last name. (example: say the last name is the second line in the txt file, I want it to search the second line of all the .txt files.) and then if there is more people with that last name, give me a list of the files it found with those names. if anyone knows how to do this it would help me a lot... thanx.
Posted
so you basically want to search a directory for a specific filename?

 

 

no... I want to search through all the txt files ina directory for something on for example the second line in the file... or third... etc.

Posted

You want to seach all files in a directory, and look if they contain a specific string?

 

It is possible, you'll have to structure your application a bit like this;

 

get all the files in the folder (see code snippet 1 below)

foreach file

{

Open the file (See code snippet 2 below)

Check if the string we look for is in this file. If so, do something with it ;).

}

 

The first snippet, getting all the files in a folder matching a specific file mask, and iterating them all. The sPathToSearch is a folder containing files, e.g. "c:\windows". sMaskToSelect is a file mask, specifying what files to select, e.g. "*.ini". With those two parameter, the Directory.GetFiles method would return a string array containing all the ini files in the windows directory.

string[] arFiles = Directory.GetFiles(sPathToSearch, sMaskToSelect);
foreach (string sFile in arFiles)
{
 //do something with the file  -> 2nd snippet.
}

 

The snippet of code belows opens a text file and checks for a specified string (sKey). The ToLower() method is used on both the original file and the key to create case insensitivity. This does take some extra CPU power though.

StreamReader objReader = File.OpenText(sFileName);
string sFileContents = objReader.ReadToEnd().ToLower();
if (sFileContents.IndexOf(sKey.ToLower(), 0, sFileContents.Length) != -1)
{
  //add this file to the list of files with the specified key
}

Nothing is as illusive as 'the last bug'.
Posted

I know when look for something in one file I would type something like this:

 

Dim sr As System.IO.StreamReader = System.IO.File.Text("Tom.txt")

 

but what would I do if I wanted to search through all the .txt files in that directory... not just Tom. How would I put that into code?

Posted

Wile just showed you.

 

This reminds me of a problem I had a long time ago. Except I programmed in C and at that time it seemed to be very difficult. How far languages have come... *sigh*

Posted
This all sounds a lot like the Grep Utility for Unix. If you need more hints and know how to read C, Grep is open source and it may help you with certian aspects of your problem. You can't get much better than grep..
  • 3 weeks later...
Posted

I found some code that is working....

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim files() As String
       Dim i As Int32

       files = System.IO.Directory.GetFiles("C:\", "*.txt")

       For i = 0 To files.Length - 1
           If SearchByLastName(files(i), "last name i want to find") Then
               Debug.WriteLine("found the last name @ '" & files(i) & "'", "Finder")
               Exit For
           End If
       Next
   End Sub

   Public Function SearchByLastName(ByVal fileName As String, ByVal lastName As String) As Boolean
       Dim streamRead As System.IO.StreamReader 'your stream reader

       Try

           If System.IO.File.Exists(fileName) = False Then
               'your file doesn't exist...
               Debug.WriteLine("File '" & fileName & "' doesn't exist.", "SearchByLastName")
               Return False
           End If

           streamRead = New System.IO.StreamReader(fileName)

           Call streamRead.ReadLine() 'Goes right to the second line.

           If streamRead.ReadLine.ToLower = lastName.ToLower Then 'read the next line in the file
               'you've found a match
               Return True
           Else
               'didn't find a match
               Return False
           End If

       Catch ex As Exception
           Debug.WriteLine(ex.ToString, "SearchByLastName")
           Return False
       Finally
           Call streamRead.Close()
       End Try

   End Function

 

I have one question about it though... This code reads the second line in the txt file only. i want to know how I would edit it to make it so that it will read like the 1st line only or the 3rd line only, etc. If anyone can help my then please post

Posted
To read a certian line number, counting through a loop might be a good way to go. Is there something a little more elegant such as "put reader here" command?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...