2012-01-13 107 views
2

我有一個在Windows XP SP3上運行的PHP 5.3.4應用程序,我需要在遠程PC上索引目錄的內容。我索引的最大目錄包含大約18,000個項目。找到包含特定項目的目錄的最快方法

此調用將找到像\\somepc.mycorp.com\foo\mydir\bar\zoo.zip這樣的項目。

// look in all the directories in \\somepc.mycorp.com\foo for directories containing a file \bar\zoo.zip 
$item_list = GetFileList('\\\\somepc.mycorp.com\\foo', '\\bar\\zoo.zip'); 

它是這樣實現的:

function GetFileList($base_dir, $path_mask) 
{ 
    $result= array(); 
    if ($handle = opendir($base_dir)) 
    { 
     while (false !== ($entry = readdir($handle))) 
     { 
      // only add items that match the mask we're looking for 
      if ($entry != "." && 
       $entry != ".." && 
       file_exists($base_dir.'\\$entry\\$path_mask')) 
      { 
       array_push($result, $entry); 
      } 
     } 

     closedir($handle); 
    } 
    return $result; 
} 

不幸的是,最大的目錄結構,這個操作可能需要一個多小時。如果我刪除過濾器,並將每個看到的項目插入到數組中,它將在幾秒鐘內完成。

有沒有更快的方法來實現這個目標?

+0

什麼是遠程PC操作系統? – 2012-01-13 15:33:46

+0

@Ivan - 一個Windows文件共享。 (操作系統可能是一些Windows服務器的變種,我不確定哪個......可能是2003 Server) – PaulH 2012-01-13 15:47:33

回答

3

我討厭拉這張卡,但是你正在做的事情可以使用bash甚至windows腳本更快地完成 - PHP可能不是這裏工作的正確工具。系統調用(甚至從PHP內部)到dir /s /bfind將爲您提供存在的所有文件的列表,然後您可以比檢查每個目錄中是否存在文件更快地使用PHP遍歷這些字符串。

我會做這在bash這樣的(因爲我懶,不知道正確的查找語法):

find | grep '/bar/zoo.zip' 

我不知道相應的Windows Shell命令(因爲我有WinGnu32 grep安裝在我的機器上),所以我無法幫到你。

編輯:

我做了一些捏造的周圍,發現Windows平臺上類似上面的命令:

dir /s /b | find "/bar/zoo.zip" 
相關問題