通常我们通过用鼠标右键单击它并选择“清空回收站”来删除回收站内容。但我有一个要求,我需要使用命令提示符删除回收站内容。这可能吗?如果是这样,我该如何实现?
14 回答
通过永久删除包含系统文件的驱动器上的回收站目录,您可以有效地从命令行“清空”回收站。(在大多数情况下,这将是C:
驱动器,但您不应该对该值进行硬编码,因为它并不总是正确的。相反,请使用%systemdrive%
环境变量。)
这种策略有效的原因是因为每个驱动器都有一个隐藏的、受保护的文件夹,名称$Recycle.bin
为 ,回收站实际存储已删除文件和文件夹的位置。删除此目录后,Windows 会自动创建一个新目录。
因此,要删除目录,请使用带有参数的rd
命令(r emove d irectory),该/s
参数表示指定目录中的所有文件和目录也应删除:
rd /s %systemdrive%\$Recycle.bin
请注意,此操作将从所有用户帐户中永久删除当前在回收站中的所有文件和文件夹。此外,您将(显然)必须从提升的命令提示符运行该命令,以获得足够的权限来执行此操作。
我更喜欢Frank P. Westlakerecycle.exe
的。它提供了一个很好的前后状态。(我使用 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.
它还有更多用途和选项(列出的输出来自 /?)。
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"
nircmd让您可以通过键入
nircmd.exe emptybin
http://www.nirsoft.net/utils/nircmd-x64.zip
http://www.nirsoft.net/utils/nircmd.html
您可以使用 powershell 脚本(这适用于具有文件夹重定向的用户以及不让他们的回收站占用服务器存储空间)
$Shell = New-Object -ComObject Shell.Application
$RecBin = $Shell.Namespace(0xA)
$RecBin.Items() | %{Remove-Item $_.Path -Recurse -Confirm:$false}
上面的脚本取自这里。
如果您有 windows 10 和 powershell 5,则有Clear-RecycleBin
commandlet。
要Clear-RecycleBin
在不确认的情况下在 PowerShell 中使用,您可以使用Clear-RecycleBin -Force
. 官方文档可以在这里找到
要悄悄删除所有内容,请尝试:
rd /s /q %systemdrive%\$Recycle.bin
我知道我参加聚会有点晚了,但我想我可能会贡献我主观上更优雅的解决方案。
我正在寻找一个脚本,它可以通过 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
这是一个更复杂的版本,它首先调用SHQueryRecycleBin()
以确定 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
我使用这个 powershell oneliner:
gci C:\`$recycle.bin -force | remove-item -recurse -force
也适用于不同于 C: 的驱动器
您可以使用此 PowerShell 命令。
Clear-RecycleBin -Force
注意:如果您需要确认提示,请删除-Force标志
尽管
rd /s /q %systemdrive%\$RECYCLE.BIN
将从系统驱动器中删除 $RECYCLE.BIN 文件夹,通常是 c:,应该考虑从任何其他可用分区中删除它,因为在本地和外部驱动器的任何分区中都有一个隐藏的 $RECYCLE.BIN 文件夹(但不在可移动驱动器,例如没有 $RECYCLE.BIN 文件夹的 USB 闪存驱动器)。例如,我在 d: 中安装了一个程序,为了删除它移动到回收站的文件,我应该运行:
rd /s /qd:\$RECYCLE.BIN
更多信息可在超级用户处从命令行清空回收站
我在批处理文件中使用这些命令来清空回收站:
del /q /s %systemdrive%\$Recycle.bin\*
for /d %%x in (%systemdrive%\$Recycle.bin\*) do @rd /s /q "%%x"
是的,您可以使用以下代码制作批处理文件:
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
这基本上创建了一个 powershell 脚本,该脚本清空 \Desktop 目录中的垃圾,然后运行它。
使用以下行创建 cmd 文件:
for %%p in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist "%%p:\$Recycle.Bin" rundll32.exe advpack.dll,DelNodeRunDLL32 "%%p:\$Recycle.Bin"
我使用EmptyRecycleBin.py
python脚本
您将需要pip install winshell
#!python3
# Empty Windows Recycle Bin
import winshell
try:
winshell.recycle_bin().empty(confirm=False, show_progress=True, sound=False)
print("Recycle Bin emptied")
except:
print('Recycle Bin is already empty')
您可以更改布尔 False 和 True 语句以打开或关闭以下内容: 确认是\否对话框、进度条、声音效果。
如果您不使用 python,那么这个用于 powershell 的单线器非常棒。
我实际上有它EmptyRecycleBin.ps1
,并在 Git Bash 中使用它。
Clear-RecycleBin -Force
所有的答案都太复杂了。OP 向 CMD 请求了一种方法来执行此操作。
给你(来自 cmd 文件):
powershell.exe /c "$(New-Object -ComObject Shell.Application).NameSpace(0xA).Items() | %%{Remove-Item $_.Path -Recurse -Confirm:$false"
是的,它将在资源管理器中更新。