Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VBS script to delete files or folders other than specified

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to use VBS script to delete files or folders other than specified". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Option Explicit

"description"

Internet Alliance-Black Fire makes it and gives it to friends in need.

'The format of the configuration file "Listfile.ini" is as follows:

'what to delete (file | directory) = folder to delete = exclusion 1; exclusion 2; exclusion 3.

'Profiles can have multiple lines to manipulate multiple directories.

'Behavioral comment lines that start with "/" in the configuration file.

'when multiple contents are excluded, separate them with a semicolon.

'example of ↓↓↓ configuration file: ↓↓↓

'/ profile start

'directory = D:\ = System Volume Information; online games; stand-alone games; Mini Game

'Directory = C:\ Program Files=qq;WinRAR

'file = D:\ online game = file 1.exe; file 2.exe

'/ end of configuration file

"explain the end"

Dim Fso,Listfile,objListfile

Listfile = "" 'set the configuration file path, if the configuration file and script are put together, please 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) = "contents" 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 finished, delete a total of" & fdnum& "directories," & flnum& "files." & vbCrLf & "time consuming" & tm& "milliseconds", 64, "execution complete"

If you do not need to display the report, comment out the line above

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 files, 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

This is the end of the content of "how to use VBS script to delete files or folders other than specified". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report