利用VBS找出本地磁盘中空的文件(夹)并删除它们

运维

  你是不是在磁盘中建立了很多空的文件或文件夹?

  你是不是在搜索它们并删除而在发愁?

  有了以下这个小程序就可以帮助你解决!

  请将以下代码用记事本保存为“找出本地磁盘中空的东西并删除.vbs” 文件! 请(不)注(要)意(说)扩(不)展(会)名哦!

  代码如下:(也可以微信”vbs_edit"回复024获取)

  Dim objfso, WshShell, ext

  Set objfso = WScript.CreateObject("Scripting.Filesystemobject")

  Set WshShell = CreateObject("Wscript.Shell")

  choices = "1.删除空的文档" & vbCr & "2.删除空的文件夹" & vbCr & "3.退出"

  prompt = "日志文档保存在 " & "C:\EmptyDelete.log" & vbCrLf & vbCrLf & "单击是(开始),否(退出)!" & vbCrLf & vbCrLf &_

   " ——VBS脚本之家"

  confirm = MsgBox("本小工具将在本地磁盘上搜索空的东西(文件夹和文件)!" & vbCr & prompt, vbYesNo +vbInformation + vbdefaultbutton1,"欢迎使用!")

  If confirm = vbyes Then

  MsgBox "不建议在C盘和D盘使用,错误删除与本作者无关——VBS脚本之家" , vbOKOnly + vbExclamation ,"提示"

   do

  getchoice = InputBox ("请输入需要处理的事项:" & vbCr & choices)

  if isnumeric(getchoice) then

  exit do

  else

  msgbox "请输入数字"

   If

  Loop

  getchoice = CInt(getchoice)

  Select Case getchoice

  Case 1: 搜索空文件

  getdrv = InputBox("请输入需要处理的盘符"& "格式如下: E:\","盘符","E")

  getdrv = getdrv & ":\"

   ext = InputBox("请输入需要搜索的文件扩展名"& "比如:txt","扩展名","txt")

  logfile = "C:\EmptyDelete.log"

  set logbook = objfso.OpenTextFile(logfile, 8, true)

  Call CheckDiskFile(getdrv,ext)

  logbook.Close

  WshShell.Popup "检查完毕!" & vbCrLf & " VBS脚本之家",5, "谢谢使用",vbInformation+vbokOnly

  Case 2: 搜索空文件夹

  getdrv = InputBox("请输入需要处理的盘符"& "格式如下: E","盘符","E")

  getdrv = getdrv & ":\"

  logfile = "C:\EmptyDelete.log"

  set logbook = objfso.OpenTextFile(logfile, 8, true)

  set drive = objfso.GetDrive(getdrv)

  CheckFolder drive.RootFolder

  logbook.Close

  WshShell.Popup "检查完毕!" & vbCrLf & " VBS脚本之家",5, "谢谢使用",vbInformation+vbokOnly

  End select

   Else If confirm = vbno Then

   MsgBox "你会回来的!" & vbCrLf & "VBS脚本之家" , vbOKOnly+ vbError,"提示"

   WScript.Quit

   End If

   End If

  Function CheckDiskFile(drv,ext)

  extTemp = ext

  On Error Resume Next

   Dim fso

   Set fso = WScript.CreateObject("Scripting.Filesystemobject")

   Set drvRootFiles = fso.GetFolder(drv)

   Set files = drvRootFiles.Files

   For Each file In files

   IsEmptyFile file,extTemp

   Next

   Set subfoldertemp = fso.GetFolder(drv)

   Set subfolders = subfoldertemp.SubFolders

   For Each subfolder In subfolders

   CheckDiskFile subfolder,extTemp 递归

   Next

  End Function

  Sub IsEmptyFile(file,ext)

   On Error Resume Next

   Set fso = CreateObject("Scripting.FileSystemObject")

  extFile = fso.GetExtensionName(file)

  If file.Size = 0 And extFile = ext Then

  ReportEmpty file

  End If

  End Sub

  Function ReportEmpty(file)

   On Error Resume Next

   response = MsgBox("我们在" & vbCr & file.Path & "发现了空文件," &_

  "你想删除吗?", vbYesNo + vbDefaultButton1,"提示")

  If vbyes = response Then

  logbook.WriteLine vbCrLf

  logbook.WriteLine "[文件:]"

  logbook.WriteLine file.Path & vbCrlf & " 在 " & Now & " 被删除"

  objfso.DeleteFile file, True

   If

  End Function

  sub CheckFolder(folderobj)

  on error resume Next

  isEmptyFolder folderobj

  for each subfolder in folderobj.subfolders

  CheckFolder subfolder

  Next

   Sub

  sub isEmptyFolder(folderobj)

  on error resume Next

  if folderobj.Size=0 and err.Number=0 then

  if folderobj.subfolders.Count=0 Then

  ReportEmptyFolder folderobj

   If

   If

   Sub

  sub ReportEmptyFolder(folderobj)

  on error resume next

  lastaccessed = folderobj.DateLastAccessed

  on error goto 0

  response = MsgBox("我们在:" & vbCr _

  & folderobj.path & vbCr & "发现了空文件夹 " & "文件夹最后访问时间:" _

  & vbCr & lastaccessed & vbCr _

  & "你想删除这个文件夹么?", _

  vbYesNoCancel + vbDefaultButton2)

  if response = vbYes Then

  logbook.WriteLine "[文件夹:]"

  logbook.WriteLine folderobj.path & vbCrlf & " 在 " & Now & " 被删除"

  folderobj.delete

  elseif response=vbCancel Then

  MsgBox "你选择了退出!谢谢使用" & vbCrLf & " VBS脚本之家"

  WScript.Quit

   If

   Sub

  ↓双击运行效果

  ↓ 输入需要处理的事项

  ↓ 输入需要清理的磁盘

  ↓ 是否清理

  打开C:\EmptyDelete.log文件,可以看到我们刚才的删除记录

  是不是很方便呢!

  欢迎微信“vbs_edit"(VBS脚本之家),学习更多关于VBS脚本知识。也可以加入我们新建立的”VBS脚本技术交流群“QQ群:572132075,欢迎广大网友加入。

标签: 运维