0

I'm looking for an string through around 20.000 to 30.000 files located in the same folder at the same level (no subfolders).

One way to do it is by using the file_get_contents function of PHP combined with strpos, but I was wondering if it is possible to use console commands for this task as unix users can do in order to improve the performance.

Right now I'm using this:

    $path = 'C:/myPath/';
    $string = 'mySearchString';

    $dir = new DirectoryIterator($path);

    $results = array();

    foreach ($dir as $file){
        if (!$file->isDot()){
            $content = file_get_contents($file->getPathname());
            if (strpos($content, $string) !== false) {
                $results[] = $file;
            }
        }
    }

I've been taking a look at how to look for a string by using windows command lines, but I can not get it working with PHP.

Community
  • 1
  • 1
Alvaro
  • 40,778
  • 30
  • 164
  • 336
  • If you are using windows, the most efficient way of doing it is having the folders included in IndexServer and do a query to it. There is information [here](http://social.msdn.microsoft.com/Forums/sqlserver/en-US/63325b03-18f9-4506-a896-958ce55bde8a/query-windows-indexing-server-with-php?forum=sqldriverforphp) about the process. – MC ND Jan 15 '14 at 10:30
  • @MCND it says *"Note Indexing Service is no longer supported as of Windows XP and is unavailable for use as of Windows 8. Instead, use Windows Search for client side search and Microsoft Search Server Express for server side search."* I guess the way to go now is through **Microsoft Search Server 2010 Express**? – Alvaro Jan 15 '14 at 11:49
  • I'm using Windows Server 2008 in the server. – Alvaro Jan 15 '14 at 11:50
  • What about using a Batch file instead and calling it from PHP? – Alvaro Jan 15 '14 at 12:17
  • Sorry for the link, you are right. I'm using the index server on the 2003 servers and Windows Search on desktops. Yes, Search Server Express is a good option, but you can not install it on a cluster, and for an installation on a domain controller a separare SQL Server installation is required. Reading 20000-30000 files with anything is time consuming. Can be done? yes. Should be done? If the queries are rare it's ok. `findstr` command or the tools in GnuWin32 project can handle it. But if the queries can be frequent, indexing is required. – MC ND Jan 15 '14 at 12:31
  • I found a faster solution by using a Batch file with `findstr` as you suggested. Answer below. – Alvaro Jan 16 '14 at 10:16

1 Answers1

0

I found a faster way by using a Batch file (.bat) and making use of the Windows console commands.

My .bat file contains the following:

@echo off
pushd "%1"
findstr /m /C:%2 *
popd

Where %1 and %2 are parameters in the PHP call to the Batch file:

$path = 'C:/my/path/';
$string = 'toSearch';
exec('cmd /c C:/path/myFile.bat '.$path.' "'.$string.'"', $result);

print_r($result);
Alvaro
  • 40,778
  • 30
  • 164
  • 336