使用腳本自動壓縮指定目標(biāo)下的所有文件的代碼
更新時間:2012年03月01日 16:24:29 作者:
有的時候,需要對一個目錄下所有的某種類型文章進(jìn)行壓縮(例如WORD文檔、MP3等)。如果使用手工,則數(shù)量少的時候還可以。如果多的話,則不勝其煩
為了解決這類問題,我使用Visual Basic Scripting設(shè)計了一個腳本,可以自動達(dá)到這個目標(biāo)。在本腳本中,自動壓縮所有文件。為了避免將腳本自己也壓縮進(jìn)去,使用了一些判斷。
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
'進(jìn)行壓縮。
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
一種更加巧妙的方法
對上個腳本稍加改動,使用正則表達(dá)式(Regular Expression ),可以方便我們的判斷過程。修改后的腳本程序如下所示。注意我們這里排除的是不壓縮的文件類型。
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
'進(jìn)行壓縮。
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
'使用正則表達(dá)式進(jìn)行判斷。
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ù)制代碼 代碼如下:
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
'進(jìn)行壓縮。
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
一種更加巧妙的方法
對上個腳本稍加改動,使用正則表達(dá)式(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
'進(jìn)行壓縮。
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
'使用正則表達(dá)式進(jìn)行判斷。
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
相關(guān)文章
SQLids.vbs 0.7(最終版,以后改成gui界面的)
今天搞了個網(wǎng)站,注入點過濾得很變態(tài),工具都不能跑,于是寫了這個東東。2009-10-10VBScript版的PHP extract()函數(shù)
這篇文章主要介紹了VBScript版的PHP extract()函數(shù),extract函數(shù)主要作用是把數(shù)組轉(zhuǎn)換成變量,非常好用的一個函數(shù),需要的朋友可以參考下2014-06-06使用 Iisext.vbs 刪除應(yīng)用程序依存關(guān)系的實現(xiàn)方法
這篇文章主要介紹了使用 Iisext.vbs 刪除應(yīng)用程序依存關(guān)系的實現(xiàn)方法,需要的朋友可以參考下2014-07-07VBS教程:VBscript屬性-HelpContext 屬性
VBS教程:VBscript屬性-HelpContext 屬性...2006-11-11