Powershell:管道文件名与特殊字符删除失败

Powershell: Pipe filename with specials chars to del fails

我正在尝试通过检查哈希来删除重复的图像,我发现这一行代码很棒,但我遇到了问题。

ls $folder\*.* -recurse  | get-filehash | group -property hash | where { $_.count -gt 1 } | % { $_.group | select -skip 1 } | del

间隔代码

ls $folder\*.* -recurse  |
get-filehash |
group -property hash |
where { $_.count -gt 1 } |
% { $_.group | select -skip 1 } |
del

如果文件名包含“[”或“]”,图像不会被删除。 我知道您可以使用 del 执行 -LiteralPath,但是它后面需要一个字符串,我不确定如何使用管道来实现它。

我忘了它是通过 Get-FileHash 和 Group-Object 传递的。试试这个:

ls $folder\*.* -recurse  |
get-filehash |
group -property hash |
where { $_.count -gt 1 } |
% { $_.group | 
    select -skip 1 | 
    % { 
        del -LiteralPath $_.Path
    } 
}

这有点奇怪,因为它会相当随意地决定保留哪个文件和删除哪个文件,但从技术上讲,这应该可行。