国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > VBScript > 正文

vbs 查找硬盤分區(qū)中指定擴(kuò)展名文件的實(shí)現(xiàn)代碼

2020-07-26 11:47:13
字體:
供稿:網(wǎng)友

挺實(shí)用的一段代碼,用來深入學(xué)習(xí)vbs,確實(shí)不錯(cuò)咯。

復(fù)制代碼 代碼如下:

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="E:/搜索結(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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 都昌县| 陆河县| 郯城县| 旌德县| 农安县| 仪陇县| 昌黎县| 射洪县| 乐东| 柯坪县| 环江| 双辽市| 乳源| 靖边县| 略阳县| 德令哈市| 古浪县| 海丰县| 久治县| 澄迈县| 多伦县| 习水县| 平南县| 迁安市| 上虞市| 蓬安县| 曲靖市| 额济纳旗| 溆浦县| 舟曲县| 西贡区| 盖州市| 永修县| 海门市| 石首市| 沭阳县| 新乐市| 龙游县| 巴青县| 岑溪市| 瓦房店市|