Empty Recycle Bin by vb script - 用VB脚本清空Windows回收站

 

If you search the VBScript newsgroup archive you can find a script that looks like this:

 

 
Const CLSID = "::{645FF040-5081-101B-9F08-00AA002F954E}"

Set oShell = CreateObject("Shell.Application")
 
Set oRecycleBin = oShell.Namespace(0).ParseName(CLSID)
oRecycleBin.InvokeVerb "Empty Recycle &Bin"

 

It uses Shell.Application object to empty recycle bin for the current user. Depending on the user settings, it is possible that the confirmation dialoq will be shown asking the user to confirm file deletion. It also displays a progress popup window. Also, depending on user regional settings, it is possible that the verb to empty recycle bin will not be named 'Empty Recycle Bin'.

Another way to do this is to use Shell.Application to get the path for each deleted file or folder, and then use FSO to delete them:

 

const ssfBITBUCKET = 10

Set objFso = CreateObject("Scripting.FileSystemObject")

Set objShell = CreateObject("Shell.Application")
Set objBitBucket = objShell.Namespace(ssfBITBUCKET)
Set colDeleted = objBitBucket.Items()

For Each objDeleted In colDeleted
    If objDeleted.Type = "File Folder" Then
        objFso.DeleteFolder(objDeleted.Path)
    Else
        objFso.DeleteFile(objDeleted.Path)
    End If
Next

 

This script will not show any dialogs, but when the files are deleted, the Recycle Bin icon on the desktop will not change to empty, and the script also works only for the current user.

On my computer (Windows XP SP 3) Recycle Bin folder paths look like this:

DriveLetter:\RECYCLER\User Account SID\

You could try to use this to delete the files in each user's Recycle Bin. You would first need to enumerate the users (using the Win32_UserAccount WMI class) and use their SIDs to construct path to each user's Recycle Bin folder. Then you would use Cim_DataFile to enumerate the files in each Recycle Bin folder and delete them. This is what the script could look like:

 

Const strRecycler = "\\RECYCLER\\"

Set objSWbemServices = GetObject _
    ("WinMgmts:Root\Cimv2")

Set colUsers = objSWbemServices.ExecQuery _
    ("Select * From Win32_UserAccount")

For Each objUser In colUsers

    Set colDisks = objSWbemServices.ExecQuery _
        ("Select * From Win32_LogicalDisk " & _
        "Where DriveType = 3")

    For Each objDisk In colDisks

        Set colDeletedFiles = objSWbemServices.ExecQuery _
        ("Select * From Cim_DataFile Where Drive = '" _
        & objDisk.DeviceId _
        & "' And Path = '" & strRecycler & objUser.Sid & "\\' " _
        & "And Hidden = False")

        For Each objDeletedFile In colDeletedFiles
            WScript.Echo objDeletedFile.Name
            'objDeletedFile.Delete
        Next
       
        Set colDeletedFolders = objSWbemServices.ExecQuery _
        ("Select * From Win32_Directory Where Drive = '" _
        & objDisk.DeviceId _
        & "' And Path = '" & strRecycler & objUser.Sid & "\\' " _
        & "And Hidden = False")

        For Each objDeletedFolder In colDeletedFolders
            WScript.Echo objDeletedFolder.Name
            'objDeletedFolder.Delete
        Next

    Next

Next


I haven't used this kind of script before, and I don't know of any issues you might find, so if you decide to go this way make sure you do enough testing. I also don't know if it will work on Windows versions other than XP SP3.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VERSION 5.00 Begin VB.Form Form1 Caption = "清空回收站" ClientHeight = 2595 ClientLeft = 60 ClientTop = 345 ClientWidth = 4680 Icon = "Form1.frx":0000 LinkTopic = "Form1" ScaleHeight = 2595 ScaleWidth = 4680 StartUpPosition = 1 '所有者中心 Begin VB.CommandButton Command1 Caption = "清空回收站" Height = 810 Left = 1020 Style = 1 'Graphical TabIndex = 1 Top = 840 Width = 1200 End Begin VB.CommandButton Command2 Caption = "退出" Height = 810 Left = 2385 TabIndex = 0 Top = 840 Width = 1200 End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False '函数声明 Private Declare Function SHEmptyRecycleBin Lib "shell32.dll" Alias "SHEmptyRecycleBinA" _ (ByVal hwnd As Long, ByVal pszRootPath As String, ByVal dwFlags As Long) As Long Private Declare Function SHUpdateRecycleBinIcon Lib "shell32.dll" () As Long Private Const SHERB_NOCONFIRMATION = &H1; Private Const SHERB_NOPROGRESSUI = &H2; Private Const SHERB_NOSOUND = &H4; Private Sub Form_Load() '为按钮加载图标 Command1.Picture = LoadPicture(App.Path & "\full.ico") End Sub Private Sub Command1_Click() Dim myval As Long ' 清空回收站 myval = SHEmptyRecycleBin(Form1.hwnd, "", SHERB_NOPROGRESSUI) If myval <> 0 Then myval = SHUpdateRecycleBinIcon() End If '为按钮加载图标 Command1.Picture = LoadPicture(App.Path & "\empty.ico") MsgBox "回收站已被清空!", vbInformation, "【提示信息】" End Sub Private Sub Command2_Click() End End Sub

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值