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
998 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
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access bios on restart 11 54
backup programme - VBA 3 24
IIF help, YN field 7 22
Help with DoEvents 8 26
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 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