找回密码
 注册账户
查看: 640|回复: 0

VBS脚本,删除指定以外的文件,文件夹

[复制链接]
棋子 发表于 2007-7-22 10:17:46 | 显示全部楼层 |阅读模式
<b>VBS脚本,删除指定以外的文件,文件夹</b><br><br>Option Explicit&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;说明&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;网盟-黑火制作,送给需要的朋友。&amp;;#39;配置文件“Listfile.ini”的格式如下:&amp;;#39;要删除什么(文件|目录)=要执行删除的文件夹=排除1;排除2;排除3............&amp;;#39;配置文件可以有多行,以便对多个目录进行操作。&amp;;#39;配置文件里以“/”开头的行为注释行。&amp;;#39;排除多个内容时,使用分号“;”进行分隔。&amp;;#39;↓↓↓ 配置文件例子:↓↓↓&amp;;#39;/配置文件开始&amp;;#39;目录=D:\=System Volume Information;网络游戏;单机游戏;小游戏&amp;;#39;目录=C:\Program Files=qq;WinRAR&amp;;#39;文件=D:\网络游戏=文件1.exe;文件2.exe&amp;;#39;/配置文件结束&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;说明完&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;&amp;;#39;Dim Fso,Listfile,objListfileListfile = &quot;&quot;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&amp;;#39;设置配置文件路径,如果配置文件和脚本放在一起,请保持原样If Listfile = &quot;&quot; Then Listfile = &quot;Listfile.ini&quot;Set Fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)On Error Resume NextSet objListfile = Fso.OpenTextFile(Listfile,1)If Err Then&nbsp; &nbsp; err.Clear&nbsp; &nbsp; Msgbox &quot;没有找到配置文件 &quot;&amp;;Listfile,16,&quot;错误&quot;&nbsp; &nbsp; WScript.quitEnd IfOn Error GoTo 0Dim flnum,fdnum,t1,t2,tmflnum=0fdnum=0t1 = timer()Dim Myline,LineArr,ListArrDo While objListfile.AtEndOfStream &lt;&gt; True&nbsp; &nbsp; Myline = LCase(Replace(objListfile.ReadLine,&quot;==&quot;,&quot;=&quot;))&nbsp; &nbsp; If Left(Myline,1) = &quot;/&quot; Then&nbsp; &nbsp; &amp;;#39;objListfile.SkipLine&nbsp; &nbsp; ElseIf CheckLine(Myline) = 2 Then&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;LineArr = Split(Myline,&quot;=&quot;)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&amp;;#39;DoFolder = LineArr(1)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ListArr = Split(LineArr(2),&quot;;&quot;)&nbsp;&nbsp;&amp;;#39;MsgBox LineArr(0)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;If LineArr(0) = &quot;目录&quot; Then DelFolder LineArr(1),ListArr&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;If LineArr(0) = &quot;文件&quot; Then DelFile LineArr(1),ListArr&nbsp; &nbsp; End IfLoopt2 = timer()tm=cstr(int(( (t2-t1)*10000 ) 0.5)/10)MsgBox &quot;扫描完毕,共删除 &quot;&amp;;fdnum&amp;;&quot; 个目录, &quot;&amp;;flnum&amp;; &quot;个文件。&quot;&amp;; vbCrLf &amp;;&quot;耗时 &quot;&amp;;tm&amp;;&quot; 毫秒&quot;,64,&quot;执行完毕&quot;&amp;;#39;不需要显示报告的话,注释掉上面这一行Set Fso=NoThingWScript.quitSub DelFolder(Folder,ListArr)Dim objFolder,subFolders,subFolder&nbsp; &nbsp; Set objFolder=Fso.Getfolder(Folder)&nbsp; &nbsp; Set subFolders=objFolder.subFolders&nbsp; &nbsp; For Each subFolder In subFolders&nbsp; &nbsp; If Not InArray(LIstArr,LCase(subFolder.name)) Then&nbsp; &nbsp;&nbsp;&nbsp;On Error Resume Next&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;subfolder.Delete(True)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;If Err Then&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;err.Clear&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Msgbox &quot;不能删除目录,请检查 &quot;&amp;;subFolder,16,&quot;错误&quot;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Else&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;fdnum = fdnum&nbsp; &nbsp;1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;End If&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;On Error GoTo 0&nbsp; &nbsp; End If&nbsp; &nbsp; NextEnd SubSub DelFile(Folder,ListArr)Dim objFolder,Files,File&nbsp; &nbsp; Set objFolder=Fso.Getfolder(Folder)&nbsp; &nbsp; Set Files=objFolder.Files&nbsp; &nbsp; For Each File In Files&nbsp; &nbsp; If Not InArray(LIstArr,LCase(File.name)) Then&nbsp; &nbsp;&nbsp;&nbsp;On Error Resume Next&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;File.Delete(True)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;If Err Then&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;err.Clear&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Msgbox &quot;不能删除文件,请检查 &quot;&amp;;File,16,&quot;错误&quot;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Else&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;flnum = flnum&nbsp; &nbsp;1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;End If&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;On Error GoTo 0&nbsp; &nbsp; End If&nbsp; &nbsp; NextEnd SubFunction CheckLine(strLine)Dim LineRegExp,MatchesSet LineRegExp = New RegExpLineRegExp.Pattern = &quot;.=.&quot;LineRegExp.Global = TrueSet Matches = LineRegExp.Execute(strLine)CheckLine = Matches.countEnd FunctionFunction InArray(Myarray,StrIn)Dim StrTempInArray = TrueFor Each StrTemp In Myarray&nbsp; &nbsp; If StrIn = StrTemp Then&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Exit Function&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Exit For&nbsp; &nbsp; End IfNextInArray = FalseEnd Function
您需要登录后才可以回帖 登录 | 注册账户

本版积分规则

存档|黑屋|手机|网络实验室 本站服务器由美国合租以及IDCLayer国际数据提供!!!

GMT+8, 2026-6-22 07:51 , Processed in 0.010206 second(s), 9 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表