在下面的脚本中,我正在搜索相同大小的文件并将它们移动到“C:\ files_compared”,问题是我想让比较文件中的一个文件所在的位置(“C:\ folder1”) )并将其他人移动到“C:\ files_compared”。
保留在原始文件夹中的文件的名称无关紧要,可以是任何比较的文件,只要它是大小比较标准中的一个。
$allfiles = Get-ChildItem -file "C:\folder1" | Group-Object -Property length
foreach($filegroup in $allfiles)
{
if ($filegroup.Count -ne 1)
{
foreach ($file in $filegroup.Group)
{
move $file.fullname "C:\files_compared"
}
}
}
谢谢。
未经测试但试试这个:
$allfiles = Get-ChildItem -file "C:\folder1" | Group-Object -Property length
foreach($filegroup in $allfiles)
{
if ($filegroup.Count -ne 1)
{
$fileGroup.Group[1..($fileGroup.Count-1)] | move -Destination 'C:\Files_compared'
}
}
一个(嵌套的)管道解决方案:
Get-ChildItem -file "C:\folder1" | Group-Object -Property length | ForEach-Object {
$_.Group | Select-Object -Skip 1 | Move-Item -Destination "C:\files_compared"
}
$_.Group
是构成给定组的所有文件的集合(相同大小的文件)。Select-Object -Skip 1
跳过集合中的第一个文件(即,将其保留在原位)并将所有其他文件(如果有)移动到目标文件夹。
这种方法不需要区分单文件组和其他文件组(代码中的$filegroup.Count -ne 1
条件),因为对于单文件组,内部管道将只是一个无操作(跳过第一个对象不会传递任何对象) Move-Item
)。