[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转贴] VBS脚本删除某些类型文件和磁盘空间报告

  1. Option Explicit
  2. dim ServerPath,onlyone,notfinddel,WorkPath,arrWorwpath,strWorkPath,fso,wsh,WshNetwork,cName,exectime,fnum,donum,totalsize,t1,t2,tm
  3. fnum=0
  4. donum=0
  5. totalsize=0
  6. WorkPath = "D:\|E:\" '这里设置需要扫描的路径(绝对路径),使用“|”来分隔多个路径
  7. serverpath = "\\10.15.0.5\itonly$\" '在这里设置服务器上的共享文件夹(可写共享),用来存放删除日志
  8. onlyone = "是" '设置是否只扫描一次,如果想每次运行都扫描,请改为“否”。
  9. notfinddel = "是" '如果本次运行没有找到任何要删除的内容,就删除日志文件,(你想每次都保留日志的话,请改为“否”)
  10. If Right(serverpath,1) <> "\" Then serverpath = serverpath&"\"
  11. arrWorwpath = Split(WorkPath,"|")
  12. t1 = timer()
  13. Set wsh = WScript.CreateObject("WScript.Shell")
  14. 'wsh.run "regsvr32 /s scrrun.dll",0,true
  15. Set fso = CreateObject("Scripting.FileSystemObject")
  16. Set WshNetwork = WScript.CreateObject("WScript.Network")
  17. cName = WshNetwork.ComputerName
  18. exectime = Now()
  19. Dim dicdrv,logfile,logfilepath,Objdrv,drvTotalSize,drvFreeSpace
  20. logfilepath = Replace(ServerPath&cName&".txt",":","-")
  21. If Not fso.FolderExists(ServerPath&cName) Then onlyone = "否"
  22. If onlyone = "否" Then
  23. Set dicdrv = CreateObject("Scripting.Dictionary")
  24. Set logfile = fso.OpenTextFile(logfilepath,8,True)
  25. logfile.WriteBlankLines(1)
  26. logfile.WriteLine "#####################################################"
  27. logfile.WriteLine "开始扫描--"&Now()
  28. logfile.WriteBlankLines(1)
  29. For Each strWorkPath In arrWorwpath
  30. If Right(strWorkPath,1) <> "\" Then strWorkPath = strWorkPath&"\"
  31. scan(strWorkPath)
  32. If Not dicdrv.Exists(UCase(Left(strWorkPath,1))&"t") Then
  33. Set Objdrv = fso.GetDrive(fso.GetDriveName(Left(strWorkPath,2)))
  34. dicdrv.add UCase(Left(strWorkPath,1))&"t",FormatNumber(Objdrv.TotalSize/1048576, 0)
  35. dicdrv.add UCase(Left(strWorkPath,1))&"f",FormatNumber(Objdrv.FreeSpace/1048576, 0)
  36. End If
  37. Next
  38. t2 = timer()
  39. tm=cstr(int(( (t2-t1)*10000 )+0.5)/10)
  40. logfile.WriteBlankLines(1)
  41. logfile.WriteLine "完成扫描,检查 "&fnum&" 个文件,共删除 "&donum&" 个文件,计 "&FormatNumber(totalsize,0)&" Kb"
  42. Dim drvkey,i
  43. drvkey = dicdrv.Keys
  44. For i = 0 To dicdrv.Count-1 Step 2
  45. logfile.WriteLine Left(drvkey(i),1)&"盘:总计磁盘空间 "&dicdrv.Item(drvkey(i))&" M ,剩余磁盘空间 "&dicdrv.Item(drvkey(i+1))&" M"
  46. Next
  47. logfile.WriteLine "耗时 " & tm & " 毫秒, "&Now()
  48. logfile.WriteLine "#####################################################"
  49. logfile.WriteBlankLines(1)
  50. logfile.close
  51. If notfinddel = "是" Then
  52. If donum = 0 Then fso.DeleteFile logfilepath,True
  53. End If
  54. If Not fso.FolderExists(ServerPath&cName) Then fso.CreateFolder(serverpath&cName)
  55. msgbox "找到 "&fnum&" 个文件"&chr(10)&"已删除 "&donum&" 个"&chr(10)&"耗时 " & tm & " 毫秒"
  56. '不需要在客户机上显示执行结果的话,注释掉上面这一行
  57. end if
  58. 'wsh.run "regsvr32 /u /s scrrun.dll",0,true
  59. Set WshNetwork = Nothing
  60. Set wsh=NoThing
  61. Set FSO=NoThing
  62. WScript.quit
  63. Sub scan(strfolder_)
  64. Dim folder_,files,file,ext,subfolders,subfolder
  65. 'on error resume next
  66. Set folder_=fso.getfolder(strfolder_)
  67. Set files=folder_.files
  68. For Each file In files
  69. fnum = fnum+1
  70. ext=fso.GetExtensionName(file)
  71. ext=lcase(ext)
  72. Select Case ext
  73. Case "mpg","wmv","mpeg","3gp","mp4","mp3","rmvb","rm" '这里是你要删除的文件类型,当然也可以是如"td","pdown"等^^文件
  74. doit(file)
  75. End Select
  76. Next
  77. set subfolders=folder_.subfolders
  78. For Each subfolder In subfolders
  79. If subfolder.name <> "System Volume Information" And subfolder.name <> "RECYCLER" Then
  80. scan(subfolder)
  81. End If
  82. Next
  83. End Sub
  84. Sub doit(file)
  85. Dim strtemp,lngsize,strsizeV
  86. strtemp = file.path
  87. lngsize = clng(file.size/1024)
  88. donum=donum+1
  89. totalsize = totalsize + lngsize
  90. 'fso.DeleteFile file,True '如果你只是想看看顾客都下了些什么,就把此行注释掉(呵呵,是不是你也想看啊)
  91. logfile.WriteLine strtemp&" -- "&FormatNumber(lngsize,0)&" Kb"
  92. dim ii
  93. for ii = 0 to lngsize step 100
  94. ii= ii + ii/8
  95. strsizeV = strsizeV & "*"
  96. next
  97. logfile.WriteLine "---"&strsizeV
  98. end Sub
  99. wscript.echo "已经完成!"
复制代码


http://lzy821218.blog.51cto.com/209800/273457

返回列表