Automatically compress the code for all files under the specified target using scripts

Time:2019-10-5

To solve these problems, I use Visual Basic Scripting to design a script that can automatically achieve this goal. In this script, all files are automatically compressed. To avoid compressing the script itself, some judgments are used.

Copy codeThe code is as follows:
call main()
Sub main()
Dim fs’file system.
Dim f ‘folder
Dim fc ‘files
Dim s ‘string
Dim ws ‘SHELL。
Dim subfs
Dim fi
‘Create SHELL.
Set ws = CreateObject(“WScript.Shell”)
‘Create file objects.
Set fs = CreateObject(“Scripting.FileSystemObject”)
Set f = fs.GetFolder(ws.currentdirectory)
Handle_files(ws.currentdirectory)
Set subfs = f.SubFolders
‘Traverse through each subdirectory.
For Each fi In subfs
Call ListSub(fi.Path)
Next
End Sub
Sub ListSub(filename)
On Error Resume Next
Dim subfs’subdirectory.
‘Processing the current directory first.
Handle_Files(filename)
‘Create file objects.
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
‘Processing files in each directory.
Sub Handle_Files(foldername)
‘Create file objects.
Set fs = CreateObject(“Scripting.FileSystemObject”)
Set f = fs.GetFolder(foldername)
Set fc = f.Files
‘Create SHELL.
Set ws = CreateObject(“WScript.Shell”)
‘Traverse file objects.
For Each fl In fc
if ((instr(fl.Name,”vbs”) = 0) and (instr(fl.Name,”rar”) = 0)) then
‘Compression.
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

A more ingenious approach
A slight change to the previous script, using regular expression, can facilitate our judgment process. The modified script is shown below. Note that we exclude uncompressed file types here.

Copy codeThe code is as follows:
call main()
Sub main()
Dim fs’file system.
Dim f ‘folder
Dim fc ‘files
Dim s ‘string
Dim ws ‘SHELL。
Dim subfs
Dim fi
‘Create SHELL.
Set ws = CreateObject(“WScript.Shell”)
‘Create file objects.
Set fs = CreateObject(“Scripting.FileSystemObject”)
Set f = fs.GetFolder(ws.currentdirectory)
Handle_files(ws.currentdirectory)
Set subfs = f.SubFolders
‘Traverse through each subdirectory.
For Each fi In subfs
Call ListSub(fi.Path)
Next
End Sub
Sub ListSub(filename)
On Error Resume Next
Dim subfs’subdirectory.
‘Processing the current directory first.
Handle_Files(filename)
‘Create file objects.
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
‘Processing files in each directory.
Sub Handle_Files(foldername)
‘Create file objects.
Set fs = CreateObject(“Scripting.FileSystemObject”)
Set f = fs.GetFolder(foldername)
Set fc = f.Files
‘Create SHELL.
Set ws = CreateObject(“WScript.Shell”)
‘Traverse file objects.
For Each fl In fc
if ( RegExpTest(“.vbs|.rar|.zip”,fl.name) = false) then
‘Compression.
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
‘Use regular expressions to make judgments.
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