溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

只能使用腳本自動(dòng)壓縮指定目標(biāo)下的所有文件

發(fā)布時(shí)間:2021-10-08 13:59:20 來(lái)源:億速云 閱讀:133 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了只能使用腳本自動(dòng)壓縮指定目標(biāo)下的所有文件,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

為了解決這類問(wèn)題,我使用Visual Basic Scripting設(shè)計(jì)了一個(gè)腳本,可以自動(dòng)達(dá)到這個(gè)目標(biāo)。在本腳本中,自動(dòng)壓縮所有文件。為了避免將腳本自己也壓縮進(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)建文件對(duì)象。
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(ws.currentdirectory)
Handle_files(ws.currentdirectory)
Set subfs = f.SubFolders
'遍歷每個(gè)子目錄。
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)建文件對(duì)象。
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
'處理每個(gè)目錄下的文件。
Sub Handle_Files(foldername)
'創(chuàng)建文件對(duì)象。
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(foldername)
Set fc = f.Files
'創(chuàng)建SHELL。
Set ws = CreateObject("WScript.Shell")
'遍歷文件對(duì)象。
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


一種更加巧妙的方法
對(duì)上個(gè)腳本稍加改動(dòng),使用正則表達(dá)式(Regular Expression ),可以方便我們的判斷過(guò)程。修改后的腳本程序如下所示。注意我們這里排除的是不壓縮的文件類型。

復(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)建文件對(duì)象。
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(ws.currentdirectory)
Handle_files(ws.currentdirectory)
Set subfs = f.SubFolders
'遍歷每個(gè)子目錄。
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)建文件對(duì)象。
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
'處理每個(gè)目錄下的文件。
Sub Handle_Files(foldername)
'創(chuàng)建文件對(duì)象。
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(foldername)
Set fc = f.Files
'創(chuàng)建SHELL。
Set ws = CreateObject("WScript.Shell")
'遍歷文件對(duì)象。
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

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“只能使用腳本自動(dòng)壓縮指定目標(biāo)下的所有文件”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI