Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching through a file

Status
Not open for further replies.

vallan

Technical User
Aug 13, 2002
156
EU
I want to achieve the following

Search through files and folders for particulars words e.g. keywords - error, critical and report back to me as an output the file and the line that report these keywords. E.g.

I have a file great.txt which has in it
Jan 21 - good
Jan 21- error
Jan 21 - critical
Jan 22 – good
Jan 22 - great
Jan 23- critical
Jan 23 - neat
Jan 24 – great.

So that when I put in the keyword “error”, it comes up with
great.txt
Jan 21 – error

And when I put in keyword “critical”, it comes up with
great.txt
Jan 21 – critical
Jan 23 – critical

Thanks


 
Vallan,

1. Use ADIR() to get the names of all the files in a given directory.

2. For each file in turn, use FILETOSTR() to read the contents of the file into a variable.

3. Use AT() to search for the string within that variable.

The above is only meant to be a rough guide. If you don't understand any of it, check the Help for the individual commands.

(I might also ask why you would want to do this, when the user has the Windows Search feature which would surely do it much better than you or I could.)

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
You might want to dig into the code for the Code References tool because it does this stuff pretty well. You can probably reuse the code there.

Tamar
 
If you need to recursively search through directories and sub-directories, I would suggest using the FileSystemObject and it's collection properties for both directories and files.



--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hello Mike,

(I might also ask why you would want to do this, when the user has the Windows Search feature which would surely do it much better than you or I could.)

Windows search is quite nice, it's really good in Vista.

But what it gives you as result are files, it's then up to you to open them and search for the actual position within the file. Or have I misunderstood a big point about windows search?

Surely the part in searching files with keyword content would better be done with an index built, like windows does, if you don't turn that off.

If you do this search with filetostr you're surely reading much data of files very unlikely to contain such keywords.
So you better limit search to some folders and file types.

More important: There may also be files containing these keywords, but in unicode or something prorietary, eg instead of "critical" then a file may contain "c r i t i c a l", with each space here representing chr(0).

That's why windows works with search providers that know the inner workings of file types and don't simply do a dumb binary search.

Bye, Olaf.
 
here's some code usable with Windows Search 3.x, (Vista)

Code:
Local loConn, loRS, loPath, loFile, lcFilename

loConn=CREATEOBJECT("adodb.connection")
loConn.ConnectionString="Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"
loConn.Open()

loRs = loConn.Execute("SELECT System.ItemFolderPathDisplay, System.Filename FROM SystemIndex WHERE CONTAINS('foxpro')")
loRS.Movefirst()
Do While Not loRS.EOF
   loPath = loRs.Fields.Item("System.ItemFolderPathDisplay")
   loFile = loRs.Fields.Item("System.Filename")

   lcFilename =  Addbs(loPath.Value)+loFile.Value
   ? lcFilename
   loRS.Movenext()
EndDo

Bye, Olaf.
 
Hi Olaf,

You've made some good points. I think the answer really depends on what Vallan is trying to achieve, and in particular whether he/she needs to search an arbitrary collection of files (that is, files of any type), or some particular files used in a given application.

I was assuming that Vallan wanted to write some code that did exactly what Windows Search does. That's what I was questioning.

Vallan,

If you're still here, perhaps you could clarify this point.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Hi All

Thank you very much for all your inputs. What I am trying to do is actually also what the search in windows does but I want to go one step further, in that I want it to read out the line in the file that actually report the search word. As you can see, the keyword happened on particular days and because of time; I will only like that date line to be sent out as a report.


Thanks
 
Hi vallan,

I understood that you want to not only find files but also the line within that. But are all those files text files? Or is that information inside other file types.

After you found files with windows search, if they are text files, you can simply read them in to find the line you need. As you want the line with the searched text in it you could either use FILETOSTR(), ALINES() and ASCAN(), I'd prefer FOPEN() and FGETS() like this:

Code:
lcSearch='critical'
lcSearch=Upper(alltrim(lcSearch))
lnFileHandle = Fopen(getfile(),0)
Do While !Feof(lnFileHandle)
   lcLine = Fgets(lnFileHandle)
   If lcSearch $ Upper(lcLine)
      ? lcLine
   Endif
EndDo 
Fclose(lnFileHandle)

This will ask you faor a file. You could replace getfile() with the filename of windows search's result.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top