windows 如何使用 PowerShell 2.0 递归删除整个目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1752677/
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
How to recursively delete an entire directory with PowerShell 2.0?
提问by Matt Spradley
What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerShell V2 in Windows 7.
在 PowerShell 中强制删除目录及其所有子目录的最简单方法是什么?我在 Windows 7 中使用 PowerShell V2。
I have learned from several sources that the most obvious command, Remove-Item $targetDir -Recurse -Force
, does not work correctly. This includes a statement in the PowerShell V2 online help (found using Get-Help Remove-Item -Examples
) that states:
我从多个来源了解到,最明显的命令Remove-Item $targetDir -Recurse -Force
不能正常工作。这包括 PowerShell V2 联机帮助(使用 找到Get-Help Remove-Item -Examples
)中的声明:
...Because the Recurse parameter in this cmdlet is faulty, the command uses the Get-Childitem cmdlet to get the desired files, and it uses the pipeline operator to pass them to the Remove-Item cmdlet...
...因为此 cmdlet 中的 Recurse 参数有问题,该命令使用 Get-Childitem cmdlet 获取所需文件,并使用管道运算符将它们传递给 Remove-Item cmdlet ...
I have seen various examples that use Get-ChildItemand pipe it to Remove-Item, but the examples usually remove some set of files based on a filter, not the entire directory.
我见过使用Get-ChildItem并将其通过管道传递给Remove-Item 的各种示例,但这些示例通常基于过滤器删除一些文件集,而不是整个目录。
I am looking for the cleanest way to blow out an entire directory, files and child directories, without generating any user warning messages using the least amount of code. A one-liner would be nice if it is easy to understand.
我正在寻找最干净的方法来清除整个目录、文件和子目录,而不会使用最少的代码生成任何用户警告消息。如果易于理解,单线会很好。
回答by Joey
Remove-Item -Recurse -Force some_dir
does indeed work as advertised here.
确实像这里宣传的那样工作。
rm -r -fo some_dir
are shorthand aliases that work too.
是也有效的速记别名。
As far as I understood it, the -Recurse
parameter just doesn't work correctly when you try deleting a filtered set of files recursively. For killing a single dir and everything below it seems to work fine.
据我了解,-Recurse
当您尝试递归删除一组过滤的文件时,该参数无法正常工作。对于杀死单个目录及其下面的所有内容似乎都可以正常工作。
回答by Tuan Jinn
I used:
我用了:
rm -r folderToDelete
This works for me like a charm (I stole it from Ubuntu).
这对我来说就像一个魅力(我从 Ubuntu 偷了它)。
回答by John Rees
When deleting files recursively using a simple Remove-Item "folder" -Recurse
I sometimes see an intermittent error : [folder] cannot be removed because it is not empty.
使用简单的递归删除文件时,Remove-Item "folder" -Recurse
我有时会看到间歇性错误:[folder] cannot be removed because it is not empty.
This answer attempts to prevent that error by individually deleting the files.
此答案试图通过单独删除文件来防止该错误。
function Get-Tree($Path,$Include='*') {
@(Get-Item $Path -Include $Include -Force) +
(Get-ChildItem $Path -Recurse -Include $Include -Force) |
sort pspath -Descending -unique
}
function Remove-Tree($Path,$Include='*') {
Get-Tree $Path $Include | Remove-Item -force -recurse
}
Remove-Tree some_dir
An important detail is the sorting of all the items with pspath -Descending
so that the leaves are deleted before the roots. The sorting is done on the pspath
parameter since that has more chance of working for providers other than the file system. The -Include
parameter is just a convenience if you want to filter the items to delete.
一个重要的细节是对所有项目进行排序,pspath -Descending
以便在根之前删除叶子。排序是在pspath
参数上完成的,因为它有更多的机会为文件系统以外的提供者工作。-Include
如果您想过滤要删除的项目,该参数只是一个方便。
It's split into two functions since I find it useful to see what I'm about to delete by running
它分为两个功能,因为我发现通过运行查看我将要删除的内容很有用
Get-Tree some_dir | select fullname
回答by stevethecollier
rm -r ./folder -Force
...worked for me
...对我来说有效
回答by steve
Try this example. If the directory does not exist, no error is raised. You may need PowerShell v3.0.
试试这个例子。如果目录不存在,则不会引发错误。您可能需要 PowerShell v3.0。
remove-item -path "c:\Test Temp\Test Folder" -Force -Recurse -ErrorAction SilentlyContinue
回答by steve
Use the old-school DOS command:
使用老式的 DOS 命令:
rd /s <dir>
回答by jdoose
For some reason John Rees' answer sometimes did not work in my case. But it led me in the following direction. First I try to delete the directory recursively with the buggy -recurse option. Afterwards I descend into every subdir that's left and delete all files.
出于某种原因,约翰里斯的回答有时对我不起作用。但它使我朝着以下方向前进。首先,我尝试使用 buggy -recurse 选项递归删除目录。之后,我进入剩下的每个子目录并删除所有文件。
function Remove-Tree($Path)
{
Remove-Item $Path -force -Recurse -ErrorAction silentlycontinue
if (Test-Path "$Path\" -ErrorAction silentlycontinue)
{
$folders = Get-ChildItem -Path $Path –Directory -Force
ForEach ($folder in $folders)
{
Remove-Tree $folder.FullName
}
$files = Get-ChildItem -Path $Path -File -Force
ForEach ($file in $files)
{
Remove-Item $file.FullName -force
}
if (Test-Path "$Path\" -ErrorAction silentlycontinue)
{
Remove-Item $Path -force
}
}
}
回答by Dejan
To avoid the "The directory is not empty" errors of the accepted answer, simply use the good old DOS command as suggested before. The full PS syntax ready for copy-pasting is:
为避免已接受答案的“目录不为空”错误,只需按照之前的建议使用旧的 DOS 命令。准备复制粘贴的完整 PS 语法是:
& cmd.exe /c rd /S /Q $folderToDelete
回答by Gajendra D Ambi
del <dir> -Recurse -Force # I prefer this, short & sweet
OR
或者
remove-item <dir> -Recurse -Force
If you have a huge directory then what I usually do is
如果你有一个巨大的目录,那么我通常做的是
while (dir | where name -match <dir>) {write-host deleting; sleep -s 3}
Run this on another powershell terminal and it will stop when it is done.
在另一个 powershell 终端上运行它,它会在完成后停止。
回答by Peter McEvoy
I took another approach inspired by @john-rees above - especially when his approach started to fail for me at some point. Basically recurse the subtree and sort files by their path-length - delete from longest to the shortest
我采取了另一种受上述@john-rees 启发的方法 - 特别是当他的方法在某些时候开始对我失败时。基本上递归子树并按路径长度对文件进行排序 - 从最长到最短删除
Get-ChildItem $tfsLocalPath -Recurse | #Find all children
Select-Object FullName,@{Name='PathLength';Expression={($_.FullName.Length)}} | #Calculate the length of their path
Sort-Object PathLength -Descending | #sort by path length descending
%{ Get-Item -LiteralPath $_.FullName } |
Remove-Item -Force
Regarding the -LiteralPath magic, here's another gotchya that may be hitting you: https://superuser.com/q/212808
关于 -LiteralPath 魔法,这里有另一个可能会打击你的陷阱:https://superuser.com/q/212808