Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
1,047 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
Independent Software Vendors: 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!

 
LVL 38

Accepted Solution

by:
PaulHews earned 1000 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 1000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

722 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