function [files,similarWords] = cdsgetfilepath(varargin)
% File search engine using Copernic Desktop Search via COM interface
%
% author: Gianluca Dorini
% email: G.Dorini@diapasonconsulting.com
%
% syntax:
%
% [files,similarWords] = cdsgetfilepath(QueryText)
%
% cdsgetfilepath submits a search query contained within the char array QueryText
% to Copernic Desktop Search. All search results that are related to files will be returned
% by means of the file's full path. cdsgetfilepath communicates with Copernic Desktop Search
% using COM interface.
%
% example: find all file about Iron Maiden
% [files,similarWords] = cdsgetfilepath('iron maiden')
%
% syntax:
%
% [files,similarWords] = cdsgetfilepath(QueryText,Conditions)
%
% The search is further constrained by binary conditions in Matlab command line format, contained
% within the string Conditions. The following variables are available:
%
% Shellcommandline char array
% Location char array
% Filename char array
% Extension char array
% Date date and time in serial date number format (double)
% Size double
%
% example: find all Iron Miden mp3
% [files,similarWords] = cdsgetfilepath('iron maiden','isequal(Extension,''.mp3'')')
%
% example: find all Iron Maiden mp3 whose size is less than 3 Mb
% [files,similarWords] = cdsgetfilepath('iron maiden','isequal(Extension,''.mp3'') && Size < 3*2^20')
%
% example: find all text files saved after March 28, 2005 3:37:07.033 PM
% [files,similarWords] = cdsgetfilepath('txt','isequal(Extension,''.txt'') && Date > datenum(''March 28, 2005 3:37:07.033 PM'')')
%
% example: find all pictures files whose name contains the words 'me' and 'her' that are in jpg format
% [files,similarWords] = cdsgetfilepath('me her','isequal(Extension,''.jpg'') || isequal(Extension,''.JPG'')');
%
% example: find all pictures files whose name contains the words 'me' and
% 'her' that are in jpg format, and that were taken on Saturday
% [files,similarWords] = cdsgetfilepath('me her',' weekday(Date) == 7 && (isequal(Extension,''.jpg'') || isequal(Extension,''.JPG''))');toc
query_Result_buffer = 100;
if nargin == 1
QueryText = varargin{1};
Conditions = '';
end
if nargin == 2
QueryText = varargin{1};
Conditions = varargin{2};
end
%connect to copernic desktop search
cds = actxserver('CopernicDesktopSearch.DesktopSearch');
% execute a query
query = ExecuteQuery(cds,QueryText);
% GetSimilarWords
similarWords = GetSimilarWords(cds,QueryText);
% check whether the quesry has been successfull
if get(query,'Valid') == 0
files = [];
similarWords = [];
return
end
% obtain the number of results
query_ResultCount = get(query,'ResultCount');
if isempty(query_ResultCount)
files = [];
similarWords = [];
return
end
files = cell(query_ResultCount,0);
k = 0;
first_index = 0;
while first_index < query_ResultCount
if (first_index + query_Result_buffer) > query_ResultCount;
query_Result_buffer = query_ResultCount - first_index;
end
% access results
query_results = GetResults(query,int32(first_index),query_Result_buffer,'shellcommandline,date,location,filename,extension,size');
% count selected results
query_results_count = get(query_results,'Count');
for i = 1:query_results_count
% extract i-th result
query_results_item = Item(query_results,i-1);
% read the number of fields for the i-th result
Item_CustomFieldCount = get(query_results_item,'CustomFieldCount');
if Item_CustomFieldCount > 0 && ~isempty(Conditions)
Shellcommandline = CustomFieldByName(query_results_item, 'Shellcommandline');
if ~isnan(Shellcommandline)
Location = CustomFieldByName(query_results_item, 'location');
Filename = CustomFieldByName(query_results_item, 'filename');
Extension = CustomFieldByName(query_results_item, 'extension');
Size = CustomFieldByName(query_results_item, 'size');
Date = CustomFieldByName(query_results_item, 'date');
Month = Date([4 5]);
Day = Date([1 2]);
Date = [Month '/' Day Date(6:end)];
Date = datenum(Date);
eval(['Condition = ' Conditions ';']);
if Condition
files{k+1,1} = Shellcommandline;
k = k+1;
end
end
end
if Item_CustomFieldCount > 0 && isempty(Conditions)
Shellcommandline = CustomFieldByName(query_results_item, 'Shellcommandline');
if ~isnan(Shellcommandline)
files{k+1,1} = Shellcommandline;
k = k+1;
end
end
end
first_index = first_index + query_Result_buffer;
end
files = files(1:k,1);