【问题标题】:Compare files sizes and move to a folder比较文件大小并移动到文件夹
【发布时间】:2018-03-02 18:24:02
【问题描述】:

在下面的脚本中,我正在搜索相同大小的文件并将它们移动到“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"
        }
    }
}

谢谢。

【问题讨论】:

    标签: powershell compare


    【解决方案1】:

    一个(嵌套的)管道解决方案:

    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 跳过集合中的第一个文件(即,将其保留在原处)并将所有其他文件(如果有)移动到目标文件夹。

      • 这种方法无需区分 1 文件组和其他组(代码中的 $filegroup.Count -ne 1 条件),因为对于 1 文件组,内部管道将只是一个空操作(跳过第一个对象离开没有对象可以传递给Move-Item)。

    【讨论】:

      【解决方案2】:

      未经测试,但试试这个:

      $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'
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多