Delete a file or folder that is not specified with the VBS script _vbs

Source: Internet
Author: User
Tags goto
Option Explicit

"" "" "" "" "" "" "" "" "
' Net Alliance-making black Fire, giving to friends in need.
The format of the ' profile ' Listfile.ini is as follows:
' What to delete (file | directory) = folder to perform deletion = exclude 1; exclude 2; Exclude 3 ...
' Profiles can have multiple lines to operate on multiple directories.
The behavior comment line that starts with "/" in the configuration file.
' When multiple content is excluded, use a semicolon '; are separated.
' ↓↓↓ configuration file Example: ↓↓↓
'/configuration file start
' Directory =d:\=system Volume information; online games; games; small games
' Directory =c:\program files=qq; WinRAR
' File =d:\ Network game = file 1.exe; file 2.exe
'/configuration file end
"" "" "" "" "" "" "" "" "

Dim Fso,listfile,objlistfile
ListFile = "" To set the profile path, if the configuration file and script are put together, leave it as is

If listfile = "" Then listfile = "Listfile.ini"
Set Fso = CreateObject ("Scripting.FileSystemObject")
On Error Resume Next
Set objlistfile = Fso.opentextfile (listfile,1)
If ERR Then
Err. Clear
Msgbox "No configuration file found" &listfile,16, "error"
Wscript.Quit
End If
On Error GoTo 0

Dim Flnum,fdnum,t1,t2,tm
Flnum=0
Fdnum=0
T1 = timer ()

Dim Myline,linearr,listarr
Do While Objlistfile.atendofstream <> True
Myline = LCase (Replace (objlistfile.readline, "=", "="))
If Left (myline,1) = "/" Then
' Objlistfile.skipline
ElseIf checkline (myline) = 2 Then
Linearr = Split (myline, "=")
' Dofolder = Linearr (1)
Listarr = Split (Linearr (2), ";")
' MsgBox Linearr (0)
If Linearr (0) = "Directory" Then Delfolder Linearr (1), Listarr
If Linearr (0) = "file" Then delfile Linearr (1), Listarr
End If
Loop

T2 = Timer ()
TM=CSTR (int ((T2-T1) *10000) +0.5)/10)

MsgBox "Scan complete, delete" &fdnum& "directory," &flnum& "file. "& VbCrLf & Time Consuming" &tm& "milliseconds", 64, "execution completed"
' Do not need to display the report, comment out the above line

Set fso=nothing
Wscript.Quit

Sub Delfolder (Folder,listarr)
Dim Objfolder,subfolders,subfolder
Set Objfolder=fso.getfolder (Folder)
Set subfolders=objfolder.subfolders
For each subfolder in subfolders
If not InArray (Listarr,lcase (subfolder.name)) Then
On Error Resume Next
subfolder. Delete (True)
If ERR Then
Err. Clear
Msgbox "Cannot delete directory, please check" &subfolder,16, "error"
Else
Fdnum = fdnum + 1
End If
On Error GoTo 0
End If
Next
End Sub

Sub Delfile (Folder,listarr)
Dim Objfolder,files,file
Set Objfolder=fso.getfolder (Folder)
Set Files=objfolder.files
For each File in Files
If not InArray (Listarr,lcase (file.name)) Then
On Error Resume Next
File.delete (True)
If ERR Then
Err. Clear
Msgbox "Cannot delete file, please check" &file,16, "error"
Else
Flnum = flnum + 1
End If
On Error GoTo 0
End If
Next
End Sub

Function Checkline (StrLine)
Dim lineregexp,matches
Set lineregexp = New RegExp
Lineregexp.pattern = ". =."
Lineregexp.global = True
Set matches = Lineregexp.execute (strLine)
Checkline = Matches.count
End Function

Function InArray (Myarray,strin)
Dim strtemp
InArray = True
For each strtemp in myarray
If Strin = strtemp Then
Exit Function
Exit for
End If
Next
InArray = False
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.