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
1,037 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 3
  • +1
18 Comments
 
LVL 75
ID: 21780381
0
 
LVL 27

Author Comment

by:jjafferr
ID: 21780489
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
ID: 21780558
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 38

Accepted Solution

by:
PaulHews earned 250 total points
ID: 21783027
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
ID: 21784364
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
ID: 21792699
0
 
LVL 27

Author Comment

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

jaffer
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 21798109
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
ID: 21801127
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
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 21806078
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
ID: 21806180

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

jaffer
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 21806196
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
ID: 21806229
0
 
LVL 27

Author Comment

by:jjafferr
ID: 21806368
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
ID: 21806915
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
ID: 21806977
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
ID: 21807155
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
ID: 21807195
Yup, provided the pdf file is searchable,
and my earlier post stands, for the iFilters.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

615 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