windows 如何通过命令提示符清空回收站?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9238953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:12:40  来源:igfitidea点击:

how to empty recyclebin through command prompt?

windowswindows-7command-linebatch-filerecycle-bin

提问by user1016403

Usually we delete the recycle bin contents by right-clicking it with the mouse and selecting "Empty Recycle Bin". But I have a requirement where I need to delete the recycle bin contents using the command prompt. Is this possible? If so, how can I achieve it?

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

回答by Cody Gray

You can effectively "empty" the Recycle Bin from the command line by permanently deleting the Recycle Bin directory on the drive that contains the system files. (In most cases, this will be the C:drive, but you shouldn't hardcode that value because it won't always be true. Instead, use the %systemdrive%environment variable.)

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

The reason that this tactic works is because each drive has a hidden, protected folder with the name $Recycle.bin, which is where the Recycle Bin actually stores the deleted files and folders. When this directory is deleted, Windows automatically creates a new directory.

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

So, to remove the directory, use the rdcommand (r?emove d?irectory) with the /sparameter, which indicates that all of the files and directories within the specified directory should be removed as well:

因此,要删除目录,请使用带有参数的rd命令 ( r? emove d?irectory) /s,这表示还应删除指定目录中的所有文件和目录:

rd /s %systemdrive%$Recycle.bin

Do note that this action will permanentlydelete all files and folders currently in the Recycle Bin from all user accounts. Additionally, you will (obviously) have to run the command from an elevated command prompt in order to have sufficient privileges to perform this action.

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

回答by wasatchwizard

I prefer recycle.exefrom Frank P. Westlake. It provides a nice before and after status. (I've been using Frank's various utilities for well over ten years..)

我更喜欢recycle.exe弗兰克·P.西湖。它提供了一个很好的前后状态。(十多年来,我一直在使用 Frank 的各种实用程序。)

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.

It also has many more uses and options (output listed is from /?).

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

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"

回答by trythis

You can use a powershell script (this works for users with folder redirection as well to not have their recycle bins take up server storage space)

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

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

The above script is taken from here.

上面的脚本取自这里

If you have windows 10 and powershell 5 there is the Clear-RecycleBincommandlet.

如果您有 Windows 10 和 powershell 5,则有Clear-RecycleBincommandlet

To use Clear-RecycleBininside PowerShell without confirmation, you can use Clear-RecycleBin -Force. Official documentation can be found here

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

回答by Robie

To stealthily remove everything, try :

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

rd /s /q %systemdrive%$Recycle.bin

回答by Oz Edri

while

尽管

rd /s /q %systemdrive%\$RECYCLE.BIN

rd /s /q %systemdrive%\$RECYCLE.BIN

will delete the $RECYCLE.BIN folder from the system drive, which is usually c:, one should consider deleting it from any other available partitions since there's an hidden $RECYCLE.BIN folder in any partition in local and external drives (but not in removable drives, like USB flash drive, which don't have a $RECYCLE.BIN folder). For example, I installed a program in d:, in order to delete the files it moved to the Recycle Bin I should run:

将从系统驱动器中删除 $RECYCLE.BIN 文件夹,通常是 c:,应该考虑从任何其他可用分区中删除它,因为在本地和外部驱动器的任何分区中都有一个隐藏的 $RECYCLE.BIN 文件夹(但不是在可移动驱动器,如 USB 闪存驱动器,没有 $RECYCLE.BIN 文件夹)。例如,我在 d: 中安装了一个程序,为了删除它移动到回收站的文件,我应该运行:

rd /s /q d:\$RECYCLE.BIN

rd /s /qd:\$RECYCLE.BIN

More information available at Super User at Empty recycling bin from command line

从命令行清空回收站的超级用户处可获得更多信息

回答by rojo

I know I'm a little late to the party, but I thought I might contribute my subjectively more graceful solution.

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

I was looking for a script that would empty the Recycle Bin with an API call, rather than crudely deleting all files and folders from the filesystem. Having failed in my attempts to RecycleBinObject.InvokeVerb("Empty Recycle &Bin")(which apparently only works in XP or older), I stumbled upon discussions of using a function embedded in shell32.dll called SHEmptyRecycleBin()from a compiled language. I thought, hey, I can do that in PowerShell and wrap it in a batch script hybrid.

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

Save this with a .bat extension and run it to empty your Recycle Bin. Run it with a /yswitch to skip the confirmation.

使用 .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


Here's a more complex version which first invokes SHQueryRecycleBin()to determine whether the bin is already empty prior to invoking SHEmptyRecycleBin(). For this one, I got rid of the choiceconfirmation and /yswitch.

这是一个更复杂的版本,它首先调用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

回答by rko281

I use this powershell oneliner:

我使用这个 powershell oneliner:

gci C:\`$recycle.bin -force | remove-item -recurse -force

Works for different drives than C:, too

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

回答by pardeew

i use these commands in a batch file to empty recycle bin:

我在批处理文件中使用这些命令来清空回收站:

del /q /s %systemdrive%$Recycle.bin\*
for /d %%x in (%systemdrive%$Recycle.bin\*) do @rd /s /q "%%x"

回答by Zazman

Yes, you can Make a Batch file with the following code:

是的,您可以使用以下代码制作批处理文件:

cd \Desktop

echo $Shell = New-Object -ComObject Shell.Application >>FILENAME.ps1
echo $RecBin = $Shell.Namespace(0xA) >>FILENAME.ps1
echo $RecBin.Items() ^| %%{Remove-Item $_.Path -Recurse -Confirm:$false} >>FILENAME.ps1


REM The actual lines being writen are right, exept for the last one, the actual thigs being writen are "$RecBin.Items() | %{Remove-Item $_.Path -Recurse -Confirm:$false}"   
But since | and % screw things up, i had to make some changes.

Powershell.exe -executionpolicy remotesigned -File  C:\Desktop\FILENAME.ps1

This basically creates a powershell script that empties the trash in the \Desktop directory, then runs it.

这基本上创建了一个 powershell 脚本,用于清空 \Desktop 目录中的垃圾,然后运行它。