【问题标题】:Compare folders sizes and move to another folder比较文件夹大小并移动到另一个文件夹
【发布时间】:2018-03-10 17:47:33
【问题描述】:

我有这个脚本可以搜索相同大小的文件,如果它的大小相同,我让一个文件在它所在的位置(“C:\folder1”)并将其他副本移动到“C:\files_compared” .

$allfiles = Get-ChildItem -file -recurse "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'
    }
}

现在的问题是我想这样做,但只使用文件夹,而不是文件。

我尝试放置 -Directory 而不是文件,但我不知道下一步该做什么。

$allfiles = Get-ChildItem -Directory -recurse "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-FileHash -Path $Path).Hash

标签: powershell compare


【解决方案1】:

您可以使用它,它比较文件夹中的文件,如果文件长度相同,它会移动到目标文件夹。

Get-ChildItem -file "E:\folder" | Group-Object -Property length | ForEach-Object {$_.Group | Select-Object -Skip 1 | Move-Item -Destination "E:\folder\move"}

【讨论】:

  • 我很感激。作为一般提示:考虑将长管道分布在多行以提高可读性,如果您在每个管道符号 (|) 后换行,则直接支持(不需要行尾转义字符。`)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多