search .txt files
search .txt files
search .txt files
search .txt files
search .txt files
search .txt files search .txt files search .txt files search .txt files search .txt files search .txt files search .txt files search .txt files
search .txt files search .txt files
search .txt files
Go Back  Xtreme Visual Basic Talk > > > search .txt files


Reply
 
Thread Tools Display Modes
  #1  
Old 02-25-2005, 12:16 PM
Talk2Tom11's Avatar
Talk2Tom11 Talk2Tom11 is offline
Centurion
 
Join Date: Dec 2003
Location: Westchester, NY
Posts: 195
Question search .txt files


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.
__________________
Ever wonder about those people who spend $2.00 a piece on those little bottles of Evian water? Try spelling Evian backwards: NAIVE.
Reply With Quote
  #2  
Old 02-25-2005, 12:29 PM
excaliber's Avatar
excalibersearch .txt files excaliber is offline
Senior Contributor

* Expert *
 
Join Date: Nov 2002
Location: Ohio, USA
Posts: 1,828
Default

Use a recursive routine (check the Code Library) to sift through all the files. Open each file for Reading (using a streamreader, binaryreader or textreader, etc). Dump the contents of the file to a buffer (string, array, etc) and search inside of that using InStr or RegEx.

If you are dealing with large files, you may want to stream the files into memory instead of reading it all at once. Stream in some arbitrary amount (50 KBytes, for instance) and search that, then stream in some more until you get to the end of the file.
__________________
RandomIRC - Your neighborhood's friendly IRC channel (irc.randomirc.com - #code)

"Perl - The only language that looks the same before and after RSA encryption."
Reply With Quote
  #3  
Old 02-25-2005, 12:57 PM
Talk2Tom11's Avatar
Talk2Tom11 Talk2Tom11 is offline
Centurion
 
Join Date: Dec 2003
Location: Westchester, NY
Posts: 195
Default

This files are about 1kb big. very small. Maybe 10-15 lines each at the most. I like the idea that was thrown out... I was just wondering how I would go about starting that... or if there is a good tutorial on it.
__________________
Ever wonder about those people who spend $2.00 a piece on those little bottles of Evian water? Try spelling Evian backwards: NAIVE.
Reply With Quote
  #4  
Old 02-26-2005, 09:29 AM
excaliber's Avatar
excalibersearch .txt files excaliber is offline
Senior Contributor

* Expert *
 
Join Date: Nov 2002
Location: Ohio, USA
Posts: 1,828
Default

__________________
RandomIRC - Your neighborhood's friendly IRC channel (irc.randomirc.com - #code)

"Perl - The only language that looks the same before and after RSA encryption."
Reply With Quote
  #5  
Old 03-01-2005, 12:50 PM
Talk2Tom11's Avatar
Talk2Tom11 Talk2Tom11 is offline
Centurion
 
Join Date: Dec 2003
Location: Westchester, NY
Posts: 195
Default

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

Code:
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?
__________________
Ever wonder about those people who spend $2.00 a piece on those little bottles of Evian water? Try spelling Evian backwards: NAIVE.
Reply With Quote
  #6  
Old 03-01-2005, 01:01 PM
by_m's Avatar
by_m by_m is offline
Contributor
 
Join Date: Jul 2004
Location: Texas
Posts: 661
Default

Make a string variable (maybe an array of strings even) to replace "Tom.txt". Then just change the value to open a different file witht the streamreader. Alternatively you could make a new streamreader (or an array of them) for each text file.
__________________
170 10101010 410 J31^170
Reply With Quote
  #7  
Old 03-01-2005, 05:48 PM
MKoslof's Avatar
MKoslofsearch .txt files MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Why not just return all .txt files in a given directory at once..(read then into an array, etc.)..and do what you have to.

Code:
Dim filesFound() As String Dim i As Long filesFound = Directory.GetFiles("C:\Testing", "*.txt") For i = 0 To filesFound.Length - 1 MessageBox.Show(filesFound(i).ToString) Next

etc
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #8  
Old 03-01-2005, 05:59 PM
Talk2Tom11's Avatar
Talk2Tom11 Talk2Tom11 is offline
Centurion
 
Join Date: Dec 2003
Location: Westchester, NY
Posts: 195
Default

Quote:
Originally Posted by MKoslof
Why not just return all .txt files in a given directory at once..(read then into an array, etc.)..and do what you have to.

Code:
Dim filesFound() As String Dim i As Long filesFound = Directory.GetFiles("C:\Testing", "*.txt") For i = 0 To filesFound.Length - 1 MessageBox.Show(filesFound(i).ToString) Next

etc

now that i have found all the files in the directory... how would look to see which one has for example there second line that reads "Smith, John"?
__________________
Ever wonder about those people who spend $2.00 a piece on those little bottles of Evian water? Try spelling Evian backwards: NAIVE.
Reply With Quote
  #9  
Old 03-02-2005, 01:23 AM
Mike210 Mike210 is offline
Newcomer
 
Join Date: Oct 2004
Posts: 22
Default

Quote:
Originally Posted by Talk2Tom11
now that i have found all the files in the directory... how would look to see which one has for example there second line that reads "Smith, John"?
I suppose you could do something like this....

Code:
Dim searchString as String = "Smith, John" If sr.ReadLine = searchString Then MessageBox.Show("Found") End If

I'm not much of a VB.NET man myself but I reckon something along those lines might work for you. (Not sure if the sr.Readline is a valid statement). You would need to open each file, and keep reading in the lines until you either find a match, or reach the end of the file, so this would be integrated into your For Next loop.

Mike.

Last edited by Mike210; 03-02-2005 at 01:30 AM.
Reply With Quote
  #10  
Old 03-02-2005, 04:43 AM
Allen G's Avatar
Allen G Allen G is offline
Centurion
 
Join Date: Jul 2004
Location: Arlington, Texas
Posts: 189
Default

Quote:
Originally Posted by Talk2Tom11
now that i have found all the files in the directory... how would look to see which one has for example there second line that reads "Smith, John"?
Run this function on each of the files in the directory, if you get a True then you've found your file

Code:
. 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
Reply With Quote
  #11  
Old 03-02-2005, 12:26 PM
Talk2Tom11's Avatar
Talk2Tom11 Talk2Tom11 is offline
Centurion
 
Join Date: Dec 2003
Location: Westchester, NY
Posts: 195
Default

Quote:
Originally Posted by Allen G
Run this function on each of the files in the directory, if you get a True then you've found your file

Code:
. 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 thing i understand the function... One question though... with this function am I going to have to know all the names of the files or will it just look for all the files that ends with .txt in a specified directory?
__________________
Ever wonder about those people who spend $2.00 a piece on those little bottles of Evian water? Try spelling Evian backwards: NAIVE.
Reply With Quote
  #12  
Old 03-02-2005, 12:31 PM
Allen G's Avatar
Allen G Allen G is offline
Centurion
 
Join Date: Jul 2004
Location: Arlington, Texas
Posts: 189
Default

Quote:
Originally Posted by Talk2Tom11
I thing i understand the function... One question though... with this function am I going to have to know all the names of the files or will it just look for all the files that ends with .txt in a specified directory?
You're going to have to know the paths of the files, then run each of your files on this until you get a True retruned.
Reply With Quote
  #13  
Old 03-02-2005, 12:42 PM
Talk2Tom11's Avatar
Talk2Tom11 Talk2Tom11 is offline
Centurion
 
Join Date: Dec 2003
Location: Westchester, NY
Posts: 195
Default

But there will be new files added to the directory everyday. I want to just search every .txt file in the directory. like I don't want to just have it search the files that I programed it to search... I want ever txt in the directory.
__________________
Ever wonder about those people who spend $2.00 a piece on those little bottles of Evian water? Try spelling Evian backwards: NAIVE.
Reply With Quote
  #14  
Old 03-02-2005, 01:21 PM
Allen G's Avatar
Allen G Allen G is offline
Centurion
 
Join Date: Jul 2004
Location: Arlington, Texas
Posts: 189
Default

Quote:
Originally Posted by Talk2Tom11
But there will be new files added to the directory everyday. I want to just search every .txt file in the directory. like I don't want to just have it search the files that I programed it to search... I want ever txt in the directory.
You're one step behind me, I thought you could already get the files? Just do something like...

Code:
Dim file as string, files as string() files = system.io.directory.getfiles("C:\yourpath\", ".txt") for each file in files if SearchByLastName(file, "last name i want to find") then debug.writeline("found the last name @ '" & file & "'".,"Finder") exit for end if next
Reply With Quote
  #15  
Old 03-02-2005, 01:33 PM
Talk2Tom11's Avatar
Talk2Tom11 Talk2Tom11 is offline
Centurion
 
Join Date: Dec 2003
Location: Westchester, NY
Posts: 195
Default

I am getting an error with the last comma in the following line:

Code:
debug.writeline("found the last name @ '" & file & "'".,"Finder")
__________________
Ever wonder about those people who spend $2.00 a piece on those little bottles of Evian water? Try spelling Evian backwards: NAIVE.
Reply With Quote
  #16  
Old 03-02-2005, 02:26 PM
Allen G's Avatar
Allen G Allen G is offline
Centurion
 
Join Date: Jul 2004
Location: Arlington, Texas
Posts: 189
Default

Quote:
Originally Posted by Talk2Tom11
I am getting an error with the last comma in the following line:

Code:
debug.writeline("found the last name @ '" & file & "'".,"Finder")
Code:
debug.writeline("found the last name @ '" & file & "'","Finder")
Reply With Quote
  #17  
Old 03-15-2005, 12:05 PM
Talk2Tom11's Avatar
Talk2Tom11 Talk2Tom11 is offline
Centurion
 
Join Date: Dec 2003
Location: Westchester, NY
Posts: 195
Default

I am still having trouble running this code. It seems to me like it is not eve finding the files in the beginning. If anyone can look at this code for me and try and run it that would be great.

Code:
Dim file As String, files As String() files = system.io.directory.getfiles("C:\yourpath\", ".txt") For Each file In files If SearchByLastName(file, "last name i want to find") Then debug.writeline("found the last name @ '" & file & "'".,"Finder") Exit For End If Next

Code:
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
__________________
Ever wonder about those people who spend $2.00 a piece on those little bottles of Evian water? Try spelling Evian backwards: NAIVE.
Reply With Quote
  #18  
Old 03-16-2005, 05:42 AM
by_m's Avatar
by_m by_m is offline
Contributor
 
Join Date: Jul 2004
Location: Texas
Posts: 661
Default

Apparently the extra period was still in there, so I'm assuming that wasn't your actual code. Nontheless, it seems that if you change the search string to "*.txt" instead of ".txt" it finds the files just fine. Then there was a problem with your "for each file in files" loop, so I just did a quick fix on it. Also, I left it in, but to check if the file exists after it has just been found seems rather ironic, I think you could safely get rid of that part.
Code:
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
__________________
170 10101010 410 J31^170
Reply With Quote
  #19  
Old 03-16-2005, 08:07 AM
tonymaclennan tonymaclennan is offline
Contributor
 
Join Date: Sep 2004
Location: Lincolnshire
Posts: 402
Default

Its just an idea and some experienced programmers may not recommend it but you could add code to a button that creates the files so it adds a line of text to an index file called something like TextFileNames.txt then use the fileopen or file stream to open this, add the file names to an array then set up a Loop which will open all the files in a folder.
Reply With Quote
  #20  
Old 03-16-2005, 08:52 PM
Talk2Tom11's Avatar
Talk2Tom11 Talk2Tom11 is offline
Centurion
 
Join Date: Dec 2003
Location: Westchester, NY
Posts: 195
Default

Thanks so much for the revised code. I have another question though. I want to know which part I need to change in the code in order to make it read the first like of the txt box or the 3 ,4 or 5. Because right now I reads only the 2nd. Yet I do like that it only does one at a time.
__________________
Ever wonder about those people who spend $2.00 a piece on those little bottles of Evian water? Try spelling Evian backwards: NAIVE.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
search .txt files
search .txt files
search .txt files search .txt files
search .txt files
search .txt files
search .txt files search .txt files search .txt files search .txt files search .txt files search .txt files search .txt files
search .txt files
search .txt files
 
search .txt files
search .txt files
 
-->