Use vbs scripts to delete files or folders not specified

Source: Internet
Author: User

Option explicit

''' Indicates ''''''''''''
'Wangmeng-heihuo production, to friends in need.
The format of 'configuration file listfile. ini 'is as follows:
'What to delete (File | directory) = folder to be deleted = exclude 1; Exclude 2; Exclude 3 ............
'The configuration file can have multiple rows to operate on multiple directories.
'Behavior comment lines starting with '/' in the configuration file.
'Use semicolons (;) to separate multiple excluded content.
'Zookeeper configuration file example: zookeeper configuration file
'/Start with the configuration file
'Directory = D: \ = system volume information; online games; single-host games; games
'Directory = c: \ Program Files = QQ; WinRAR
'File = D: \ Network Game license file 1.exelicense file 2.exe
'/End of configuration file
''''''''''''

Dim FSO, listfile, objlistfile
Listfile = "" 'sets the configuration file path. If the configuration file and script are put together, keep them as they are.

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 "configuration file not 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)

After msgbox is scanned, a total of "& fdnum &" directories and "& flnum &" files are deleted. "& Vbcrlf &" Time consumed "& TM &" millisecond ", 64," execution completed"
'The report does not need to be displayed. 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 directories. 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 "file cannot be deleted. 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
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.