一個(gè)用vbs查找硬盤所有分區(qū)中的指定程序的代碼
更新時(shí)間:2008年06月03日 11:27:13 作者:
用vbs實(shí)現(xiàn)可以查找指定擴(kuò)展名的文件,方便大家搜索
On Error Resume Next '忽略所有錯(cuò)誤
Dim filename '聲明變量
Dim re
Set re=New RegExp '建立正則表達(dá)式對象實(shí)例
re.Pattern="^([a-z]|[A-Z])+\:\\\w+\.vbs$"
If re.Test(WScript.ScriptFullName)=False Then
MsgBox "請?jiān)诖疟P根目錄下運(yùn)行本程序,否則搜索結(jié)果可能會(huì)不正確!",,"MessageBox"
WScript.Quit
End If
re.Pattern="^([A-Za-z0-9_]|[^\x00-\xff])+\.[a-zA-Z]{1,4}$" '聲明正則表達(dá)式的匹配模式,主要用來檢驗(yàn)用戶輸入的文件名是否正確
Do
filename=InputBox("請輸入你要搜索的文件名:","MessageBox")
If filename="" Then WScript.Quit '如果輸入為空則退出腳本
If re.Test(filename)=False Then
MsgBox "請輸入合法的文件名!",,"MessageBox"
End If
Loop While re.Test(filename)=False '直到用戶輸入正確的文件名時(shí)才跳出循環(huán)。
Set re=Nothing
Dim ie
Set ie=WScript.CreateObject("internetexplorer.application") '建立IE對象,用來顯示搜索狀態(tài)
ie.menubar=0 '不顯示IE對象菜單欄
ie.AddressBar=0 '不顯示IE對象地址欄
ie.ToolBar=0 '不顯示IE對象工具欄
ie.StatusBar=0 '不顯示IE對象狀態(tài)欄
ie.FullScreen=1 '全屏化IE對象
ie.Width=640 '設(shè)置IE對象寬度
ie.Height=120 '設(shè)置IE對象高度
ie.Resizable=0 '設(shè)置IE對象大小是否可以被改動(dòng)
ie.Navigate "about:blank" '設(shè)置IE對象默認(rèn)指向的頁面
ie.Left=Fix((ie.Document.parentwindow.screen.availwidth-ie.Width)/2) '設(shè)置IE對象左邊距
ie.top=Fix((ie.document.parentwindow.screen.availheight-ie.height)/2) '設(shè)置IE對象右邊距
ie.visible=1 '設(shè)置IE對象是否可視
With ie.Document '以下為在IE對象中寫入頁面,跟一般的HTML沒有區(qū)別
.write "<html>"
.write "<head>"
.write "<title>文件掃描狀態(tài)</title>"
.write "<meta http-equiv=""content-type"" content=""text/html;charset=gb2312"">"
.write "<style><!--"
.write "body { background:#000000;text-align:center;margin:0px auto; }"
.write "* { font-family:Arial;font-size:9pt;color:#00cc00;line-height:140%; }"
.write "a:link,ahover,a:visited { text-decoration:none; }"
.write "#scanstatus { text-align:left;margin:15px; }"
.write "#header { width:100%;height:20px; }"
.write "#middle { width:100%;height:50px; }"
.write "#footer { width:100%;height:20px;text-align:right; }"
.write "--></style>"
.write "</head>"
.write "<body scroll=no>"
.write "<div id=""scanstatus"">"
.write "<div id=""header"">正在啟動(dòng)搜索程序。。。</div>"
.write "<div id=""middle""></div>"
.write "<div id=""footer""><a href=""#"" onclick=""window.close()"">退出程序</a></div>"
.write "</div>"
.write "</body>"
.write "</html>"
End With
'定義文件系統(tǒng)對象變量
Dim fso
Dim objfolder
Dim objsubfolders
Dim objsubfolder
Dim objfiles
Dim objfile
Dim objdrives
Dim objdrive
Dim objtextfile
Dim str:str=""
Dim i:i=0 '計(jì)數(shù)器變量
Dim result
result="C:\搜索結(jié)果.html" '搜索結(jié)果保存文件變量
'一個(gè)過程,用來遍歷硬盤文件
Function search(path)
Set objfolder=fso.getfolder(path) '獲得當(dāng)前路徑
Set objfiles=objfolder.Files '獲得當(dāng)前路徑下的所有文件集合
For Each objfile In objfiles '開始遍歷文件集合
ie.Document.getElementById("middle").innerHTML=objfile.Path '用到IE對象的文檔對象模型,將當(dāng)前搜索的文件路徑寫入ID為middle的DIV中
If objfile.Name=filename Then '如果當(dāng)前文件名與用戶輸入的文件名一致
i=i+1 '計(jì)數(shù)器加一
str=str & objfile.Path & "<br>"
Set objtextfile=fso.OpenTextFile(result,2,True) '創(chuàng)建文本流對象,文件名為變量result所存儲(chǔ)的字符串
objtextfile.Write(str) '將變量str中的文件路徑寫入html文件中
objtextfile.Close '關(guān)閉文本流對象
Set objtextfile=Nothing '銷毀對象
End If
If i>0 Then
ie.Document.getElementById("header").innerHTML="找到 " & i & " 個(gè)匹配,詳細(xì)信息已保存在 """ & result & """ 文件中。。。"
Else
ie.Document.getElementById("header").innerHTML="正在執(zhí)行文件搜索。。。"
End If
WScript.Sleep(20)
Next
Set objsubfolders=objfolder.SubFolders '得到當(dāng)前路徑下的所有文件夾的集合
For Each objsubfolder In objsubfolders '遍歷文件夾
nowpath=path & "\" & objsubfolder.Name '得到新的文件路徑
search nowpath '調(diào)用函數(shù)自身,從新的路徑開始搜索
Next
End Function
Set fso=CreateObject("scripting.filesystemobject")
Set objdrives=fso.Drives '取得當(dāng)前計(jì)算機(jī)的所有磁盤驅(qū)動(dòng)器
For Each objdrive In objdrives '遍歷磁盤
search objdrive '調(diào)用函數(shù)
Next
'結(jié)束時(shí)顯示的信息
ie.Document.getElementById("header").innerHTML="掃描已結(jié)束。。。"
If i>0 Then
ie.Document.getElementById("middle").innerHTML="請打開 """ & result & """ 查看詳細(xì)搜索結(jié)果!"
Else
ie.Document.getElementById("middle").innerHTML="沒有找到要搜索的文件!"
End If
'銷毀對象變量,釋放內(nèi)存空間
Set objdrives=Nothing
Set objfiles=Nothing
Set objfile=Nothing
Set objdrive=Nothing
Set objfolders=Nothing
Set objfolder=Nothing
Set objsubfolders=Nothing
Set objsubfolder=Nothing
Set fso=Nothing
Dim filename '聲明變量
Dim re
Set re=New RegExp '建立正則表達(dá)式對象實(shí)例
re.Pattern="^([a-z]|[A-Z])+\:\\\w+\.vbs$"
If re.Test(WScript.ScriptFullName)=False Then
MsgBox "請?jiān)诖疟P根目錄下運(yùn)行本程序,否則搜索結(jié)果可能會(huì)不正確!",,"MessageBox"
WScript.Quit
End If
re.Pattern="^([A-Za-z0-9_]|[^\x00-\xff])+\.[a-zA-Z]{1,4}$" '聲明正則表達(dá)式的匹配模式,主要用來檢驗(yàn)用戶輸入的文件名是否正確
Do
filename=InputBox("請輸入你要搜索的文件名:","MessageBox")
If filename="" Then WScript.Quit '如果輸入為空則退出腳本
If re.Test(filename)=False Then
MsgBox "請輸入合法的文件名!",,"MessageBox"
End If
Loop While re.Test(filename)=False '直到用戶輸入正確的文件名時(shí)才跳出循環(huán)。
Set re=Nothing
Dim ie
Set ie=WScript.CreateObject("internetexplorer.application") '建立IE對象,用來顯示搜索狀態(tài)
ie.menubar=0 '不顯示IE對象菜單欄
ie.AddressBar=0 '不顯示IE對象地址欄
ie.ToolBar=0 '不顯示IE對象工具欄
ie.StatusBar=0 '不顯示IE對象狀態(tài)欄
ie.FullScreen=1 '全屏化IE對象
ie.Width=640 '設(shè)置IE對象寬度
ie.Height=120 '設(shè)置IE對象高度
ie.Resizable=0 '設(shè)置IE對象大小是否可以被改動(dòng)
ie.Navigate "about:blank" '設(shè)置IE對象默認(rèn)指向的頁面
ie.Left=Fix((ie.Document.parentwindow.screen.availwidth-ie.Width)/2) '設(shè)置IE對象左邊距
ie.top=Fix((ie.document.parentwindow.screen.availheight-ie.height)/2) '設(shè)置IE對象右邊距
ie.visible=1 '設(shè)置IE對象是否可視
With ie.Document '以下為在IE對象中寫入頁面,跟一般的HTML沒有區(qū)別
.write "<html>"
.write "<head>"
.write "<title>文件掃描狀態(tài)</title>"
.write "<meta http-equiv=""content-type"" content=""text/html;charset=gb2312"">"
.write "<style><!--"
.write "body { background:#000000;text-align:center;margin:0px auto; }"
.write "* { font-family:Arial;font-size:9pt;color:#00cc00;line-height:140%; }"
.write "a:link,ahover,a:visited { text-decoration:none; }"
.write "#scanstatus { text-align:left;margin:15px; }"
.write "#header { width:100%;height:20px; }"
.write "#middle { width:100%;height:50px; }"
.write "#footer { width:100%;height:20px;text-align:right; }"
.write "--></style>"
.write "</head>"
.write "<body scroll=no>"
.write "<div id=""scanstatus"">"
.write "<div id=""header"">正在啟動(dòng)搜索程序。。。</div>"
.write "<div id=""middle""></div>"
.write "<div id=""footer""><a href=""#"" onclick=""window.close()"">退出程序</a></div>"
.write "</div>"
.write "</body>"
.write "</html>"
End With
'定義文件系統(tǒng)對象變量
Dim fso
Dim objfolder
Dim objsubfolders
Dim objsubfolder
Dim objfiles
Dim objfile
Dim objdrives
Dim objdrive
Dim objtextfile
Dim str:str=""
Dim i:i=0 '計(jì)數(shù)器變量
Dim result
result="C:\搜索結(jié)果.html" '搜索結(jié)果保存文件變量
'一個(gè)過程,用來遍歷硬盤文件
Function search(path)
Set objfolder=fso.getfolder(path) '獲得當(dāng)前路徑
Set objfiles=objfolder.Files '獲得當(dāng)前路徑下的所有文件集合
For Each objfile In objfiles '開始遍歷文件集合
ie.Document.getElementById("middle").innerHTML=objfile.Path '用到IE對象的文檔對象模型,將當(dāng)前搜索的文件路徑寫入ID為middle的DIV中
If objfile.Name=filename Then '如果當(dāng)前文件名與用戶輸入的文件名一致
i=i+1 '計(jì)數(shù)器加一
str=str & objfile.Path & "<br>"
Set objtextfile=fso.OpenTextFile(result,2,True) '創(chuàng)建文本流對象,文件名為變量result所存儲(chǔ)的字符串
objtextfile.Write(str) '將變量str中的文件路徑寫入html文件中
objtextfile.Close '關(guān)閉文本流對象
Set objtextfile=Nothing '銷毀對象
End If
If i>0 Then
ie.Document.getElementById("header").innerHTML="找到 " & i & " 個(gè)匹配,詳細(xì)信息已保存在 """ & result & """ 文件中。。。"
Else
ie.Document.getElementById("header").innerHTML="正在執(zhí)行文件搜索。。。"
End If
WScript.Sleep(20)
Next
Set objsubfolders=objfolder.SubFolders '得到當(dāng)前路徑下的所有文件夾的集合
For Each objsubfolder In objsubfolders '遍歷文件夾
nowpath=path & "\" & objsubfolder.Name '得到新的文件路徑
search nowpath '調(diào)用函數(shù)自身,從新的路徑開始搜索
Next
End Function
Set fso=CreateObject("scripting.filesystemobject")
Set objdrives=fso.Drives '取得當(dāng)前計(jì)算機(jī)的所有磁盤驅(qū)動(dòng)器
For Each objdrive In objdrives '遍歷磁盤
search objdrive '調(diào)用函數(shù)
Next
'結(jié)束時(shí)顯示的信息
ie.Document.getElementById("header").innerHTML="掃描已結(jié)束。。。"
If i>0 Then
ie.Document.getElementById("middle").innerHTML="請打開 """ & result & """ 查看詳細(xì)搜索結(jié)果!"
Else
ie.Document.getElementById("middle").innerHTML="沒有找到要搜索的文件!"
End If
'銷毀對象變量,釋放內(nèi)存空間
Set objdrives=Nothing
Set objfiles=Nothing
Set objfile=Nothing
Set objdrive=Nothing
Set objfolders=Nothing
Set objfolder=Nothing
Set objsubfolders=Nothing
Set objsubfolder=Nothing
Set fso=Nothing
相關(guān)文章
從一個(gè)VBS腳本學(xué)習(xí)一點(diǎn)點(diǎn)東西
從一個(gè)VBS腳本學(xué)習(xí)一點(diǎn)點(diǎn)東西...2006-09-09
ass2srt.vbs(ass/ssa批量轉(zhuǎn)換srt的腳本)
這篇文章主要介紹了ass2srt.vbs一個(gè)ass/ssa批量轉(zhuǎn)換srt的腳本,需要的朋友可以參考下2014-09-09
用vbs實(shí)現(xiàn)自動(dòng)檢查代理是否可用,并自動(dòng)設(shè)置IE代理的腳本
每次用代理網(wǎng)站,找了很多代理地址,大部分是不可以用的,但是可不可用,要手工一個(gè)個(gè)的試,有點(diǎn)麻煩,下面代碼幫你解決這個(gè)問題2008-06-06
使用vbs腳本來監(jiān)控windows服務(wù)器上的應(yīng)用程序(不存在就啟動(dòng))
這個(gè)vbs代碼主要實(shí)現(xiàn)的功能就是運(yùn)行該程序,就會(huì)在進(jìn)程中出現(xiàn)一個(gè)wscript.exe?它會(huì)每隔10s掃面一次進(jìn)程中是否存在notepad.exe這個(gè)程序,不存在就啟動(dòng)。這個(gè)啟動(dòng)程序可能跟進(jìn)程名不一樣,好比tomcat應(yīng)用,啟動(dòng)的是startup.bat,后臺(tái)進(jìn)程名為java.exe2023-06-06
vbs下用一個(gè)小方法實(shí)現(xiàn)批量添加域用戶
在域模式下批量添加域用戶就有些困難,下面是一個(gè)vbs腳本,在同級目錄下用放置一個(gè)users.txt的文本文檔,格式:username,password,在dc上運(yùn)行該腳本,即可以將users.txt的用戶信息批量添加到ad里面??梢酝ㄟ^移動(dòng)或拖拽的方法,將生成的用戶移動(dòng)到ou下面,以保證ou的組策略及文件夾重定向?qū)υ撚脩羯А?2008-06-06
VBS 十六進(jìn)制異或加密實(shí)現(xiàn)代碼
文件不要過大,最好不要超過 100 KB,密鑰不要用數(shù)字,不想改了。理論上支持任何二進(jìn)制文件格式2013-07-07

