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

首頁 > 編程 > VBScript > 正文

使用腳本自動壓縮指定目標(biāo)下的所有文件的代碼

2019-10-26 18:05:33
字體:
供稿:網(wǎng)友
為了解決這類問題,我使用Visual Basic Scripting設(shè)計了一個腳本,可以自動達到這個目標(biāo)。在本腳本中,自動壓縮所有文件。為了避免將腳本自己也壓縮進去,使用了一些判斷。
復(fù)制代碼 代碼如下:

call main()
Sub main()
Dim fs '文件系統(tǒng)。
Dim f 'folder
Dim fc 'files
Dim s 'string
Dim ws 'SHELL。
Dim subfs
Dim fi
'創(chuàng)建SHELL。
Set ws = CreateObject("WScript.Shell")
'創(chuàng)建文件對象。
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(ws.currentdirectory)
Handle_files(ws.currentdirectory)
Set subfs = f.SubFolders
'遍歷每個子目錄。
For Each fi In subfs
Call ListSub(fi.Path)
Next
End Sub
Sub ListSub(filename)
On Error Resume Next
Dim subfs '子目錄。
'首先處理當(dāng)前目錄。
Handle_Files(filename)
'創(chuàng)建文件對象。
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(filename)
Set subfs = f.SubFolders
For Each fi In subfs
Call ListSub(fi.Path)
Next
End Sub
'處理每個目錄下的文件。
Sub Handle_Files(foldername)
'創(chuàng)建文件對象。
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(foldername)
Set fc = f.Files
'創(chuàng)建SHELL。
Set ws = CreateObject("WScript.Shell")
'遍歷文件對象。
For Each fl In fc
if ((instr(fl.Name,"vbs") = 0) and (instr(fl.Name,"rar") = 0)) then
'進行壓縮。
s = "winrar M -ep " & fl.Path & ".rar " & fl.Path
ws.Run s, 0, True
End If
Next
End Sub
sub output(string)
wscript.echo string
end sub

一種更加巧妙的方法
對上個腳本稍加改動,使用正則表達式(Regular Expression ),可以方便我們的判斷過程。修改后的腳本程序如下所示。注意我們這里排除的是不壓縮的文件類型。
復(fù)制代碼 代碼如下:

call main()
Sub main()
Dim fs '文件系統(tǒng)。
Dim f 'folder
Dim fc 'files
Dim s 'string
Dim ws 'SHELL。
Dim subfs
Dim fi
'創(chuàng)建SHELL。
Set ws = CreateObject("WScript.Shell")
'創(chuàng)建文件對象。
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(ws.currentdirectory)
Handle_files(ws.currentdirectory)
Set subfs = f.SubFolders
'遍歷每個子目錄。
For Each fi In subfs
Call ListSub(fi.Path)
Next
End Sub
Sub ListSub(filename)
On Error Resume Next
Dim subfs '子目錄。
'首先處理當(dāng)前目錄。
Handle_Files(filename)
'創(chuàng)建文件對象。
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(filename)
Set subfs = f.SubFolders
For Each fi In subfs
Call ListSub(fi.Path)
Next
End Sub
'處理每個目錄下的文件。
Sub Handle_Files(foldername)
'創(chuàng)建文件對象。
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(foldername)
Set fc = f.Files
'創(chuàng)建SHELL。
Set ws = CreateObject("WScript.Shell")
'遍歷文件對象。
For Each fl In fc
if ( RegExpTest(".vbs|.rar|.zip",fl.name) = false) then
'進行壓縮。
s = "winrar M -ep " & fl.Path & ".rar " & fl.Path
output s
ws.Run s, 0, True
End If
Next
End Sub
sub output(string)
wscript.echo string
end sub
'使用正則表達式進行判斷。
Function RegExpTest(patrn, strng)
Dim regEx, retVal ' Create variable.
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = False ' Set case sensitivity.
retVal = regEx.Test(strng) ' Execute the search test.
If retVal Then
RegExpTest = true
Else
RegExpTest = false
End If
End Function
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 信阳市| 百色市| 襄城县| 平凉市| 普兰店市| 育儿| 德惠市| 酒泉市| 沙湾县| 昆山市| 竹山县| 杭锦后旗| 堆龙德庆县| 潜江市| 太谷县| 繁峙县| 宾川县| 西林县| 沁源县| 渝北区| 怀来县| 托克逊县| 蒲江县| 普兰县| 汽车| 射阳县| 慈溪市| 平果县| 威海市| 东光县| 揭西县| 永川市| 庐江县| 勃利县| 稻城县| 安义县| 华阴市| 河东区| 定南县| 武陟县| 巫溪县|