Question

I'm trying to get the Windows Indexing search work directly over PHP so I can search for text inside thousands of files very quickly.

I have it working on Visual Basic with this script:

'To run this snippet, save it to a file and run it using cscript.exe from a command line. 
'Running the .vbs file with Windows Script Host may cause dialog boxes to open for each item returned from the index.

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

objConnection.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"

objRecordSet.Open "SELECT System.ItemName FROM SYSTEMINDEX WHERE DIRECTORY='file:C:/folderIndexed' AND CONTAINS('myDemo') ORDER BY System.ItemName DESC", objConnection

Do Until objRecordset.EOF
    Wscript.Echo objRecordset.Fields.Item("System.ItemName")
    objRecordset.MoveNext
Loop

Now I'm trying to port it into PHP by using the COM class as suggested here but I'm getting this error message:

com_exception
Source: Unknown
Description: Unknown

My attempt looks like so:

<?php
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn->Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';");

$recordset = new COM("ADODB.Recordset");  

$keyword = 'a';
$sql = "SELECT filename, size, path
     FROM SCOPE()
     WHERE  DIRECTORY='file:C:/folderIndexed' and CONTAINS('a')";


//-----------------> line of the error <-----------------
$recordset = $recordset->Open($sql, $conn);


foreach ($recordset as $obj) { 
    echo  $obj->Fields->Item("System.filename")->Value,  "\ n" ;  
} 

What am I doing wrong?

I'm using PHP 5.5. and I'm using extension=php_com_dotnet.dll at php.ini. The php_com_dotnet.dll file is placed at the ext folder as detailed at the extension_dir value.



Related question:

Was it helpful?

Solution

$recordset->open does not return a RecordSet, you should not be assigning the return value as you are doing.

Try changing:

$recordset = $recordset->Open($sql, $conn);

To

$recordset->Open($sql, $conn);

In addition, your ported code significantly deviates from structure of your VBScript version. Here is my attempt at porting your code, which works for me.

$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$recordset = new COM("ADODB.Recordset");  

$conn->Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';");

// your query doesn't work for me in VBSCRIPT or PHP. this one does.
$recordset->Open("SELECT Top 5 System.ItemPathDisplay FROM SYSTEMINDEX", $conn);

if(!$recordset->EOF) $recordset->MoveFirst();
while(!$recordset->EOF) {
    echo $recordset->Fields->Item("System.ItemPathDisplay")->Value . "\n";
    $recordset->MoveNext();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top