如何通过命令提示符清空recyclebin?

use*_*403 76 windows recycle-bin command-line batch-file windows-7

通常我们通过用鼠标右键单击并选择"清空回收站"来删除回收站内容.但我有一个要求,我需要使用命令提示符删除回收站内容.这可能吗?如果是这样,我该如何实现呢?

Cod*_*ray 85

通过永久删除包含系统文件的驱动器上的"回收站"目录,可以从命令行有效地"清空"回收站.(在大多数情况下,这将是C:驱动器,但您不应该对该值进行硬编码,因为它并不总是正确的.而是使用%systemdrive%环境变量.)

这种策略的工作原因是因为每个驱动器都有一个带有名称的隐藏受保护文件夹$Recycle.bin,这是回收站实际存储已删除文件和文件夹的位置.删除此目录时,Windows会自动创建新目录.

因此,删除目录,使用rd命令([R EMOVE d irectory)与/s参数,这表明所有的指定目录中的文件和目录也应该被删除:

rd /s %systemdrive%\$Recycle.bin
Run Code Online (Sandbox Code Playgroud)

请注意,此操作将从所有用户帐户中永久删除当前在回收站中的所有文件和文件夹.此外,您(显然)必须从提升的命令提示符运行该命令,以便具有足够的权限来执行此操作.

  • 还有一些注意事项:**•**在您实际打开回收站和/或刷新桌面之前,bin状态的更改可能不会反映在Explorer(桌面图标)中,**•**仅影响该特定卷; 其他驱动器上的回收文件不会受到影响,因此您可能实际上没有使用此方法清空*回收站,并且**•**目录名可能因Windows版本而异(我相信文件系统也是如此).它可能是`$ Recycle.bin`,`Recycled`,`Recycler`等等,如果像Norton Recovery Bin这样的多启动程序有自己的目录,你甚至可能有多个. (17认同)
  • 注意:如果您在脚本中使用它,您还需要传入一个`/ q`开关,因此`rd`不会给您额外的提示.`rd/s/q%SYSTEMDRIVE%\ $ Recycle.bin` (3认同)

was*_*ard 25

我更喜欢recycle.exe弗兰克·P.西湖.它提供了一个很好的前后状态.(我已经使用弗兰克的各种公用事业十多年了..)

C:\> recycle.exe /E /F
Recycle Bin: ALL
    Recycle Bin C:  44 items, 42,613,970 bytes.
    Recycle Bin D:   0 items, 0 bytes.
            Total:  44 items, 42,613,970 bytes.

Emptying Recycle Bin: ALL
    Recycle Bin C:   0 items, 0 bytes.
    Recycle Bin D:   0 items, 0 bytes.
            Total:   0 items, 0 bytes.
Run Code Online (Sandbox Code Playgroud)

它还有更多的用途和选项(列出的输出来自/?).

Recycle all files and folders in C:\TEMP:
  RECYCLE C:\TEMP\*

List all DOC files which were recycled from any directory on the C: drive:
  RECYCLE /L C:\*.DOC

Restore all DOC files which were recycled from any directory on the C: drive:
  RECYCLE /U C:\*.DOC

Restore C:\temp\junk.txt to C:\docs\resume.txt:
  RECYCLE /U "C:\temp\junk.txt" "C:\docs\resume.txt"

Rename in place C:\etc\config.cfg to C:\archive\config.2007.cfg:
  RECYCLE /R "C:\etc\config.cfg" "C:\archive\config.2007.cfg"
Run Code Online (Sandbox Code Playgroud)

  • @Pacerier内置方式!?喜欢用键盘和/或鼠标导航Windows资源管理器?使用桌面上的图标(假设回收站甚至显示在桌面上)?这两个都没有解决op的问题.我真的认为在某个地方有一个用于清空垃圾箱的命令,比如删除历史记录`RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1`但我还没找到它.所以,如果您有内置方式,请分享! (2认同)

Mwi*_*iza 11

您可以使用此 PowerShell 命令。

Clear-RecycleBin -Force
Run Code Online (Sandbox Code Playgroud)

注意:如果您想要确认提示,请删除-Force标志

  • 有效,给出错误:`Clear-RecycleBin:系统找不到指定的路径在 line:1 char:1 + Clear-RecycleBin -Force + ~~~~~~~~~~~~~~~~~~ ~~~~~ + CategoryInfo : InvalidOperation: (RecycleBin:String) [Clear-RecycleBin], Win32Exception + ExcellentQualifiedErrorId : FailedToClearRecycleBin,Microsoft.PowerShell.Commands.ClearRecycleBinCommand` (2认同)

小智 9

您可以使用powershell脚本(这适用于具有文件夹重定向的用户,也不会使其回收站占用服务器存储空间)

$Shell = New-Object -ComObject Shell.Application
$RecBin = $Shell.Namespace(0xA)
$RecBin.Items() | %{Remove-Item $_.Path -Recurse -Confirm:$false}
Run Code Online (Sandbox Code Playgroud)

上面的脚本取自这里.

如果你有windows 10和powershell 5,那就有一个Clear-RecycleBin命令行开关.

要在Clear-RecycleBin没有确认的情况下使用内部PowerShell,您可以使用Clear-RecycleBin -Force.官方文档可以在这里找到

  • 清除RecycleBin在Windows 10中的PowerShell中很有用 (2认同)

小智 6

我使用这个 powershell oneliner:

gci C:\`$recycle.bin -force | remove-item -recurse -force
Run Code Online (Sandbox Code Playgroud)

也适用于与 C: 不同的驱动器


小智 5

要悄悄删除所有内容,请尝试:

rd /s /q %systemdrive%\$Recycle.bin
Run Code Online (Sandbox Code Playgroud)


roj*_*ojo 5

我知道我参加聚会有点晚了,但我想我可能会贡献我主观上更优雅的解决方案。

我正在寻找一个脚本,它可以通过 API 调用清空回收站,而不是从文件系统中粗暴地删除所有文件和文件夹。我的尝试失败了RecycleBinObject.InvokeVerb("Empty Recycle &Bin")(这显然只适用于 XP 或更早版本),我偶然发现了关于使用SHEmptyRecycleBin()从编译语言调用的 shell32.dll 中嵌入的函数的讨论。我想,嘿,我可以在 PowerShell 中做到这一点,并将其包装在批处理脚本混合中。

使用 .bat 扩展名保存并运行它以清空您的回收站。使用/y开关运行它以跳过确认。

<# : batch portion (begins PowerShell multi-line comment block)
:: empty.bat -- http://stackoverflow.com/a/41195176/1683264

@echo off & setlocal

if /i "%~1"=="/y" goto empty

choice /n /m "Are you sure you want to empty the Recycle Bin? [y/n] "
if not errorlevel 2 goto empty
goto :EOF

:empty
powershell -noprofile "iex (${%~f0} | out-string)" && (
    echo Recycle Bin successfully emptied.
)
goto :EOF

: end batch / begin PowerShell chimera #>
Add-Type shell32 @'
    [DllImport("shell32.dll")]
    public static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath,
        int dwFlags);
'@ -Namespace System

$SHERB_NOCONFIRMATION = 0x1
$SHERB_NOPROGRESSUI = 0x2
$SHERB_NOSOUND = 0x4

$dwFlags = $SHERB_NOCONFIRMATION
$res = [shell32]::SHEmptyRecycleBin([IntPtr]::Zero, $null, $dwFlags)

if ($res) { "Error 0x{0:x8}: {1}" -f $res,`
    (New-Object ComponentModel.Win32Exception($res)).Message }
exit $res
Run Code Online (Sandbox Code Playgroud)

这是一个更复杂的版本,它首先调用SHQueryRecycleBin()以确定在调用 .bin 之前 bin 是否已经为空SHEmptyRecycleBin()。对于这个,我去掉了choice确认和/y切换。

<# : batch portion (begins PowerShell multi-line comment block)
:: empty.bat -- http://stackoverflow.com/a/41195176/1683264

@echo off & setlocal
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF

: end batch / begin PowerShell chimera #>
Add-Type @'

using System;
using System.Runtime.InteropServices;

namespace shell32 {
    public struct SHQUERYRBINFO {
        public Int32 cbSize; public UInt64 i64Size; public UInt64 i64NumItems;
    };

    public static class dll {
        [DllImport("shell32.dll")]
        public static extern int SHQueryRecycleBin(string pszRootPath,
            out SHQUERYRBINFO pSHQueryRBInfo);

        [DllImport("shell32.dll")]
        public static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath,
            int dwFlags);
    }
}
'@

$rb = new-object shell32.SHQUERYRBINFO

# for Win 10 / PowerShell v5
try { $rb.cbSize = [Runtime.InteropServices.Marshal]::SizeOf($rb) }
# for Win 7 / PowerShell v2
catch { $rb.cbSize = [Runtime.InteropServices.Marshal]::SizeOf($rb.GetType()) }

[void][shell32.dll]::SHQueryRecycleBin($null, [ref]$rb)
"Current size of Recycle Bin: {0:N0} bytes" -f $rb.i64Size
"Recycle Bin contains {0:N0} item{1}." -f $rb.i64NumItems, ("s" * ($rb.i64NumItems -ne 1))

if (-not $rb.i64NumItems) { exit 0 }

$dwFlags = @{
    "SHERB_NOCONFIRMATION" = 0x1
    "SHERB_NOPROGRESSUI" = 0x2
    "SHERB_NOSOUND" = 0x4
}
$flags = $dwFlags.SHERB_NOCONFIRMATION
$res = [shell32.dll]::SHEmptyRecycleBin([IntPtr]::Zero, $null, $flags)

if ($res) { 
    write-host -f yellow ("Error 0x{0:x8}: {1}" -f $res,`
        (New-Object ComponentModel.Win32Exception($res)).Message)
} else {
    write-host "Recycle Bin successfully emptied." -f green
}
exit $res
Run Code Online (Sandbox Code Playgroud)