You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于文件时长删除重复文件并保留最早原文件(支持指定路径与目录)

Fixing Your PowerShell Duplicate File Remover: Add Path Support & Directory Traversal

Great start using file hashes to detect duplicates—this is the most accurate method since it checks actual file content, not just filenames or sizes. Let's tweak your script to support a target path, traverse subdirectories recursively, and ensure only the earliest original file is kept for each set of duplicates.

Here's the revised, fully functional script:

param(
    [Parameter(Mandatory=$false)]
    [string]$TargetPath = (Get-Location).Path
)

# Get all files in target path and subdirectories, calculate their SHA256 hash
$files = Get-ChildItem -Path $TargetPath -File -Recurse | 
         Select-Object FullName, CreationTime, 
             @{Name='FileHash'; Expression={(Get-FileHash -Path $_.FullName -Algorithm SHA256).Hash}}

# Group files by their hash (duplicates share the same hash)
$duplicateGroups = $files | Group-Object -Property FileHash | Where-Object {$_.Count -gt 1}

# Process each group: keep the earliest file, delete the rest
foreach ($group in $duplicateGroups) {
    # Sort files by creation time (oldest first)
    $sortedFiles = $group.Group | Sort-Object -Property CreationTime

    # Keep the first (earliest) file, mark the rest for deletion
    $keepFile = $sortedFiles[0]
    $deleteFiles = $sortedFiles | Select-Object -Skip 1

    Write-Host "`nKeeping original file (created $($keepFile.CreationTime)): $($keepFile.FullName)"
    Write-Host "Marking $($deleteFiles.Count) duplicate(s) for deletion:"
    
    foreach ($file in $deleteFiles) {
        Write-Host "- $($file.FullName) (created $($file.CreationTime))"
        # Uncomment the line below to actually delete files (test first!)
        # Remove-Item -Path $file.FullName -Force -Confirm:$false
    }
}

Write-Host "`nDuplicate cleanup process completed."

Key Improvements & Explanations:

  • Specify a Target Path: The param block lets you pass a path like E:\media\ when running the script. If no path is provided, it defaults to your current working directory.
  • Recursive Directory Traversal: The -Recurse flag in Get-ChildItem ensures we scan every subdirectory under the target path.
  • Reliable Duplicate Detection: Using Get-FileHash with SHA256 guarantees that only files with identical content are flagged as duplicates (avoids false positives from same-size different-content files).
  • Preserve the Earliest File: Each group of duplicates is sorted by CreationTime, so the oldest file is kept, and all newer copies are marked for deletion.

How to Use:

  1. Save the script as Remove-Duplicates.ps1
  2. Open PowerShell, navigate to the script's location
  3. Run it with your target path:
    .\Remove-Duplicates.ps1 -TargetPath "E:\media\"
    
  4. Important: Test first! The deletion line is commented out—run the script once to verify which files will be removed, then uncomment the Remove-Item line when you're confident.

Safety Tips:

  • Always back up critical files before running duplicate deletion scripts.
  • If you need to scan system-protected directories, run PowerShell as Administrator.
  • You can modify the hash algorithm (e.g., MD5) if needed, but SHA256 is more secure and less prone to collisions.

内容的提问来源于stack exchange,提问作者Juan Lopez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:41:42