To solve this problem, I used visual Basic scripting to design a script that would automatically achieve this goal. In this script, all files are compressed automatically. To avoid compressing the script itself, some judgments are used.
Copy Code code 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 the shell.
Set ws = CreateObject ("Wscript.Shell")
' Create a File object.
Set fs = CreateObject ("Scripting.FileSystemObject")
Set f = fs. GetFolder (Ws.currentdirectory)
Handle_files (Ws.currentdirectory)
Set SUBFS = f.subfolders
' Iterate 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.
' Process the current directory first.
Handle_files (filename)
' Create a File object.
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
' Process the files in each directory.
Sub Handle_files (foldername)
' Create a File object.
Set fs = CreateObject ("Scripting.FileSystemObject")
Set f = fs. GetFolder (FolderName)
Set FC = F.files
' Create the shell.
Set ws = CreateObject ("Wscript.Shell")
' Traverses the file object.
For each FL in FC
if (InStr (fl. Name, "vbs") = 0) and (InStr (FL). Name, "rar") = 0) Then
' To compress.
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 expressions (Regular Expression), can facilitate our judgment process. The modified script is shown below. Note that we exclude file types that are not compressed.
Copy Code code 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 the shell.
Set ws = CreateObject ("Wscript.Shell")
' Create a File object.
Set fs = CreateObject ("Scripting.FileSystemObject")
Set f = fs. GetFolder (Ws.currentdirectory)
Handle_files (Ws.currentdirectory)
Set SUBFS = f.subfolders
' Iterate 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.
' Process the current directory first.
Handle_files (filename)
' Create a File object.
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
' Process the files in each directory.
Sub Handle_files (foldername)
' Create a File object.
Set fs = CreateObject ("Scripting.FileSystemObject")
Set f = fs. GetFolder (FolderName)
Set FC = F.files
' Create the shell.
Set ws = CreateObject ("Wscript.Shell")
' Traverses the file object.
For each FL in FC
if (Regexptest (". vbs|. rar|. Zip ", fl.name) = False) Then
' To compress.
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 for judgment.
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