I am not a big forum guy but I just got hooked on writing a script to do the following: accept a user's query, feed it to Windows Search, and display the found files in a paper folder in XYPlorer. Since I spent an inordinate length of time getting it to work, I thought I might as well share my code. For those that like the speed and convenience of the indexed search in Windows, it might be useful.
- It is possible to write a simple VBScript to query Windows Search using "Windows Search SQL" (See the "ADO and VBScript" section here).
- Such a script can be run from a command prompt using cscript.exe, and the resulting list of file paths can be directed into a text file.
- The SQL query syntax is unwieldy for a human to enter, but it contains a variety of potentially useful search features (start here).
- Some of those potentially useful features--such as automatically matching inflected word forms using FORMSOF--I could not get to work right.
- What I could get to work are the following: i) partial string matching using LIKE in file name searches; ii) simple fulltext searching using FREETEXT; and iii) fulltext searching with boolean operators and prefix matching using CONTAINS. You'll have to check out the links I've provided above if you want to dig into the details of this SQL search syntax.
- Take a user-friendly query.
- Change it into the form required by the SQL syntax.
- Create a WinSearch.vbs file containing the VBScript (this file is created inside XYPlorer's scripts folder).
- Call cscript.exe from the command prompt to run the VBScript, and return the list of found files to XYPlorer.
- Create a paper folder called "SearchResults" containing these results, and display its contents.
- WinSearch.xys: contains two scripts: FREETEXT and CONTAINS (see below for what they do).
- ProcessNameQuery.xys: loaded by the CONTAINS script to convert the user-supplied query into SQL form for name search.
- ProcessContentsQuery.xys: loaded by the CONTAINS script to convert the query in to SQL form for contents search.
I keep these three scripts in my <xypath>\Scripts directory and use them by creating a catalog item that does ::load WinSearch. Then when I click on this item I get the option to do a FREETEXT or CONTAINS search.
FREETEXT takes a simple query that is only composed of words that should appear in the document or file name, example queries could be <findthis>, <findthis findthat>, or <findthis "this phrase">. You can also specify a single extension to restrict the search by filetype, as in <findthis *.pdf>. Further instructions and comments are given in the query input box that pops up when you run the script.
CONTAINS takes more complex queries, such as <this that>, <this not that>, <this near that>, <(this and that) not theother *.txt>, and <(this or "this phrase") and oth*>. Boolean operator capitalization and missing ANDs, etc. are taken care of by the script, so that query entry is user-friendly. You may also include one file extension (e.g. *.txt) as in the previous case. The CONTAINS search is first run on file names only, where partial string matching will work (e.g. "cat" will find "scat" and "educate"). The file name matches are placed at the top of the results list. Search is then run again in full text mode, where the contents of the documents are searched. Matches from this search are placed beneath the filename matches.
In both cases the resulting paper folder shows search results sorted by the search engine's ranking. You can view the VBScript code for running the search in the WinSearch.vbs file that's created in the scripts directory.
Finally, my disclaimers: I have very limited previous experience with both of VBScript and XYPlorer scripting, so perhaps I've done something less efficiently than is possible. I have this working well on my Win 8.1 x64 system, but perhaps others will have problems I haven't encountered. Very likely you will be able to feed the script queries that it will choke on, but I've put a timeout of 10 seconds on the cscript.exe call so any bad queries should not cause major problems. I provide this code only as a courtesy to others that might find it useful, so please enjoy, use at your own risk, and modify as you see fit.
Oh and finally finally: my compliments to XYPlorer's developer for authoring such an awesome piece of software that can be user-extended in this way!