我有几千个重复文件(以jar文件为例),我想使用powershell
我对powershell很陌生,我正把它扔给那些可能会帮上忙的PS人员。
发布于 2016-03-07 18:46:05
试试这个:
ls *.txt -recurse | get-filehash | group -property hash | where { $_.count -gt 1 } | % { $_.group | select -skip 1 } | del
来自:http://n3wjack.net/2015/04/06/find-and-delete-duplicate-files-with-just-powershell/
发布于 2013-05-30 21:08:35
保存文件字典,在前面已经遇到下一个文件名时删除:
$dict = @{};
dir c:\admin -Recurse | foreach {
$key = $_.Name #replace this with your checksum function
$find = $dict[$key];
if($find -ne $null) {
#current file is a duplicate
#Remove-Item -Path $_.FullName ?
}
$dict[$key] = 0; #dummy placeholder to save memory
}
我使用了文件名作为密钥,但如果需要(或两者兼用),可以使用校验和-请参阅代码注释。
发布于 2014-12-19 12:47:41
尽管这个问题很老,但我一直需要根据内容清理所有重复的文件。这个想法很简单,这方面的算法并不简单。下面是接受要删除重复项的“路径”参数的代码。
Function Delete-Duplicates {
param(
[Parameter(
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True
)]
[string[]]$PathDuplicates)
$DuplicatePaths =
Get-ChildItem $PathDuplicates |
Get-FileHash |
Group-Object -Property Hash |
Where-Object -Property Count -gt 1 |
ForEach-Object {
$_.Group.Path |
Select -First ($_.Count -1)}
$TotalCount = (Get-ChildItem $PathDuplicates).Count
Write-Warning ("You are going to delete {0} files out of {1} total. Please confirm the prompt" -f $DuplicatePaths.Count, $TotalCount)
$DuplicatePaths | Remove-Item -Confirm
}
脚本
( a)列出所有ChildItems
( b)从它们中检索FileHash
( c)按Hash属性对它们进行分组(因此所有相同的文件都在单个组中)
( d)过滤掉已经唯一的文件(组-eq 1的计数)
( e)遍历每个组并列出除最后路径之外的所有路径--确保每个"Hash“的一个文件始终保持不变。
( f)在前面发出警告,说明总共有多少文件和要删除的文件有多少。
可能不是最符合性能的选项(SHA1-ing每个文件),但确保文件是重复的。对我来说非常好:)
https://stackoverflow.com/questions/16845674
复制相似问题