Solved

Windows Search "A word or ohrase in the file" API, Search in for a string in a file

Posted on 2008-06-13
18
965 Views
Last Modified: 2008-06-17
Hi guys
Is there an API for the windows search in files?
this includes MS Office files, pdf, txt, ...etc.
OR
How can I send s string to this windows search, with the path, and ask it to do the search?

TIA

jaffer
0
Comment
Question by:jjafferr
  • 8
  • 5
  • 3
  • +1
18 Comments
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
0
 
LVL 27

Author Comment

by:jjafferr
Comment Utility
Hi mx

This link is to search for a file,
but I want to search for a string in the file, like:
Window's Search "A word or ohrase in the file".

jaffer
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
oops ...

Well, maybe you could open the file (once found with that API) ... and then do the search ?  Otherwise, I really don't know - but someone probably will.

mx
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 250 total points
Comment Utility
There is no API to run the search.  Using ShellExecute, you can launch the search, but passing parameters can only be done with simulating key strokes (as far as I know.)

Simple example follows:
Option Explicit

Private Declare Function ShellExecute Lib "Shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As VbAppWinStyle) As Long

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()

    ShellExecute 0, "find", "C:\", vbNullString, vbNullString, vbNormalFocus

    SendKeys "{tab}this is my phrase" 'Tab is to move to the next input box.

    Sleep 1000

    SendKeys "{Enter}"

    

End Sub

Open in new window

0
 
LVL 27

Author Comment

by:jjafferr
Comment Utility
Hi Paul

The code worked like a charm, thanks :)
just incease the sleep to 2000.

Can we capture the results,
or better yet,
can we make this search as a control in a Form?

jaffer
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
Comment Utility
0
 
LVL 27

Author Comment

by:jjafferr
Comment Utility
Thanks Erik, I will look at it in a day or two, looks promissing though :)

jaffer
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
Comment Utility
Well you got me, How did you manage to call me by my first name? humble..

I looked at the API for shell and I came up with this snippet.

This is based off the idea of saving a .fnd file and passing the path to the .fnd file. I am also sure you could use shellexecute to point to the file itself which would accomplish the same task by filling in the edit boxes. However if ths needs to be something you want to search on the fly then this is prob not the way to go because you need to save the search file first from the search dialog.

Option Explicit

' http://support.microsoft.com/kb/308885

Private Declare Function ShFindFiles Lib "shell32" Alias "#90" ( _
    ByVal pidlRoot As Long, _
    ByVal pidlSavedSearch As Long) As Long
   
Private Declare Function SHParseDisplayName Lib "shell32.dll" ( _
    ByVal pszName As Long, _
    ByVal pbc As Long, _
    ByRef ppidl As Long, _
    ByVal sfgaoIn As Long, _
    ByRef psfgaoOut As Long) As Long

Private Function FindFileSearch(ByVal fndFile As String) As Long
    Dim lpIDList        As Long
    SHParseDisplayName StrPtr(fndFile), 0, lpIDList, 0, 0
    ShFindFiles 0, lpIDList
End Function

Private Sub Command1_Click()
FindFileSearch ("c:\calc.fnd")
End Sub
0
 
LVL 27

Author Comment

by:jjafferr
Comment Utility
Hi Erik

You are famous, just google your name :)

Thanks for the code, but since you are famous for classes and wrapers, is it possible to capture/save the search result even in a txt file, so that I can retrieve it to a textbox in the Form?

This would be the ideal solution for me.
.fnd saves the search critera and path, but not the results.

jaffer
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
Comment Utility
The following works for me with Windows Search 4.0

Dim con As ADODB.Connection
Dim rst As ADODB.Recordset

Set con = New ADODB.Connection
Set rst = New ADODB.Recordset
 
Dim sConString As String
Dim sSQLString As String

sConString = "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"
con.Open sConString
'The following example searches for documents containing the word "software", and the phrase "computer".
sSQLString = "SELECT System.ItemPathDisplay FROM SYSTEMINDEX WHERE FREETEXT(*,'warcraft ""wow""')"

Set rst = con.Execute(sSQLString)

Do Until (rst.EOF)
   Debug.Print rst.Fields.Item("System.ItemPathDisplay").Value
   rst.MoveNext
Loop

rst.Close
Set rst = Nothing

con.Close
Set con = Nothing
0
 
LVL 27

Author Comment

by:jjafferr
Comment Utility

what does Windows Search 4.0 require?
I am now on winXp home edition.

jaffer
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
Comment Utility
I added a reference to Microsoft ActiveX Data Objects 2.8 ( for the above snippet)

You may need to update your index locations which can be located in
Control Panel :::: Indexing Options
0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 250 total points
Comment Utility
0
 
LVL 27

Author Comment

by:jjafferr
Comment Utility
Erik

Thanks for the info and the code,
but I cannot make the users install additional software on their PC's,
my laptop is up to date with windows updates, but it doesn't have Windows Search 4.0, so it doesn't even get downloaded as part of windows update.

I was hoping to use the existing windows search facility, which every user would have by default.

I am sure you have tried your best, so did Paul,
so I guess it is only fair to close this Q, sharing the points between both of you guys.

Thank you Paul, Erik,
it was nice of you to share your knowledge :)

jaffer
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
It's interesting that MS has started making the Windows search accessible through code.  Does the windows search include PDF files and Office files?
0
 
LVL 27

Author Comment

by:jjafferr
Comment Utility
Yes, both PDF and Office files are included,
there is something called iFilters http://ifilter.org , which specifies the file types, so if your pc doesn't search for certain files, add the iFilter for that particular file.

jaffer
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
I wasn't very clear... I meant does it allow searching for a string inside those types of files.
0
 
LVL 27

Author Comment

by:jjafferr
Comment Utility
Yup, provided the pdf file is searchable,
and my earlier post stands, for the iFilters.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now