我大幅修改了程式碼, 可以在查詢中看到完全的狀況,
不會再感覺到[當(卡)在那邊]了...
你再試試看
語法:
<HTML lang=zh-TW>
<HEAD>
<TITLE>本機 檔案搜尋</TITLE>
<META http-equiv=Content-Type content="text/html; charset=big5">
</HEAD>
<style>
.Field {color: black; border: 1px solid #FFFFFF; background-color: #FFCC00;}
.Find {color:blue; font: 12px Arial;}
</style>
<script>
var Fo = new ActiveXObject("Scripting.FileSystemObject");
var StrOut = new String();
var FileName = new String();
var Extention = new String();
var TableBarIndex = 0;
var cwvdavid_fileRead = new ActiveXObject("Scripting.FileSystemObject");
var SearchING = false;
var FindDirArray; //要搜尋的全部目錄資料...
var MissDirectory = ["_vti_", "images", "flash"]; //要忽略的目錄的開頭名稱(請寫小寫), 可自行再加入更多...
var myThreading_InterVal;
var commandList = [];
function executeCommands() { if(commandList.length>0) commandList.shift()(); }
function FindFile(FOo)
{
FindDirArray = [];
GetAllDirectory(FOo); //記錄所有要查詢的目錄...
var FOo = Fo.GetFolder(FindDirArray.shift()); //一個目錄一個目錄的執行...
simThread(FOo, null);
}
function GetAllDirectory(FOo)
{
var SearchThisDirectory = true;
var i;
for(i = 0; i<MissDirectory.length; i++)
{
if(FOo.Name.toLowerCase().indexOf(MissDirectory[i]) == 0) //如果這個目錄 要忽略掉...就不記錄
{
SearchThisDirectory = false;
}
}
if(SearchThisDirectory)
{
FindDirArray.push(FOo.path);
var subFOo = new Enumerator(FOo.SubFolders); //取得該目錄下的所有目錄...
for( ; !subFOo.atEnd(); subFOo.moveNext())
{
GetAllDirectory(Fo.GetFolder(subFOo.item().Path)); //使用遞迴.....來取得全部的目錄...
}
}
}
function simThread(FOo, FSo)
{
if(FSo == null)
{
FSo = new Enumerator(FOo.Files);
}
if(!FSo.atEnd()) //還沒到最後一個[檔案]之前...
{
//路徑的尾巴要加\, 不然找不到
mypath = FOo.Path;
//檢查mypath的尾巴是否有\, 如果沒有就加上去...
if(mypath.lastIndexOf("\\")+1 != mypath.length)
{
mypath = mypath + "\\";
}
window.status = "搜尋中... " + mypath + FSo.item().name; //把查詢過程顯示在IE的狀態列
//檢查這個檔案的檔名...看有沒有符合...查詢的條件...
if(FileName == "*" || FSo.item().name.slice(0,FSo.item().name.lastIndexOf(".")).toLowerCase().indexOf(FileName)>-1)
{
if(Extention == "*" || FSo.item().name.slice(FSo.item().name.lastIndexOf(".")+1).toLowerCase().indexOf(Extention)>-1)
{
//把找到的檔案列出來...
ts = cwvdavid_fileRead.OpenTextFile(mypath + FSo.item().name, 1);
myReadWord = ts.ReadAll();
if(myReadWord.indexOf(searchWord.value) != -1)
{
//如果有找到文字...就列出該檔案
NewTR = Result_Table.insertRow(Result_Table.rows.length);
NewTR.style.backgroundColor = ((TableBarIndex % 2)? "#FFFFFF":"#DDAA55");
NewTD = NewTR.insertCell(0);
NewTD.innerHTML = mypath;
NewTD.className = "Find";
NewTD = NewTR.insertCell(1);
NewTD.innerHTML = "<a target=\"_blank\" href=\"" + mypath + FSo.item().name + "\">" + FSo.item().name + "</a>";
NewTD.className = "Find";
NewTD = NewTR.insertCell(2);
NewTD.innerHTML = FSo.item().type;
NewTD.className = "Find";
NewTD = NewTR.insertCell(3);
NewTD.innerHTML = String(FSo.item().size/(1024*1024)).slice(0,3) +" MB";
NewTD.className = "Find";
TableBarIndex++; //列出檔案時, 將變數加1, 以便換顏色...
}
ts.Close();
}
}
FSo.moveNext(); //移到下一個檔案...
commandList.push(function(){simThread(FOo, FSo);});
}
else //找下一個目錄的資料...
{
if(FindDirArray.length>0)
{
simThread(Fo.GetFolder(FindDirArray.shift()), null);
}
else
{
alert("已搜尋完畢...");
mySearchBTN.value = " 開始搜尋 ";
SearchING = false;
clearInterval(myThreading_InterVal);
}
}
}
function Scan()
{
if(SearchING)
{
clearInterval(myThreading_InterVal);
mySearchBTN.value = " 開始搜尋 ";
SearchING = false;
alert("已中斷搜尋...");
}
else
{
myThreading_InterVal = setInterval("executeCommands()",1); //每一毫秒就執行一次...
SearchING = true;
mySearchBTN.value = "(搜尋中...) 立即停止搜尋";
FileName = (search.value.lastIndexOf(".")>-1)? search.value.slice(0,search.value.lastIndexOf(".")):(search.value.length>0)? search.value.toLowerCase():"*"; //Get Searched File Name
Extention = (search.value.lastIndexOf(".")>-1)? search.value.slice(search.value.lastIndexOf(".")+1).toLowerCase():"*"; // Get Searched File Extention Name
TableBarIndex = 0;
if(path.value.length>0 && Fo.FolderExists(path.value))
{
tableRows = Result_Table.rows.length;
for(i=1; i<tableRows; i++) Result_Table.deleteRow(1); //保留標題列, 其他結果清除...
FindFile(Fo.GetFolder(path.value));
}
else
{
alert("錯誤的路徑...");
}
}
}
</script>
<BODY topmargin="0" leftmargin="0">
<table border=0 width=100% cellspacing="0" style="border-collapse: collapse" cellpadding="2"><tr>
<td dir="ltr" bgcolor="#FFCC00"><b><font face="Arial" size="2">Named :</font></b></td>
<td dir="ltr" bgcolor="#FFCC00">
<input size=50 type=text id=search name=search class="Field" value="*.html"></td>
</tr><tr>
<td dir="ltr" bgcolor="#FFCC00">
<p dir="ltr"><b><font face="Arial" size="2">Path : </font></b> </td>
<td bgcolor="#FFCC00">
<input size=50 type=text value="D:\" id=path name=path class="Field" ></td>
</tr><tr>
<td bgcolor="#FFCC00">要找的文字...</td>
<td bgcolor="#FFCC00">
<input size=50 type=text value="文字" id=path0 name=searchWord class="Field" ></td>
</tr><tr>
<td bgcolor="#FFCC00"> </td>
<td bgcolor="#FFCC00">
<input type=button id=mySearchBTN value=" 開始搜尋 " onclick=Scan() class="Field"></td>
</tr><tr>
<td colspan=2 align=right bgcolor="#FFCC00"><font face=arial size=2><b>Search Result</b></font><hr></td>
</tr><tr>
<td colspan=2 bgcolor="#FFCC00">
<div id=outPut>
<table border=0 width=100% cellspacing=0 id="Result_Table">
<tr id="ResultTR1" bgcolor=\"#CCCCCC\"" +"><td class=find width=20%>路徑</td><td class=find width=30%>檔名</td><td class=find width=25%>檔案類型</td><td class=find width=50%>檔案大小</td></tr>
</table>
</div>
</td>
</tr></table>
</BODY>
</HTML>
(呼...寫這種程式會爆肝.....
以後要少碰......)