Use scripts to automatically compress code for all files under a specified target _vbs

Source: Internet
Author: User
Tags rar
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.