当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Windows:使用 PowerShell 查找并消除重复文件

作者:精品下载站 日期:2024-12-14 07:32:54 浏览:12 分类:玩电脑

Windows:使用 PowerShell 查找并消除重复文件


我们生活在一个大数据世界,这既是福也是祸。大数据通常意味着大量的照片和视频等文件,最终意味着巨大的存储空间。文件被意外或故意地从一个位置移动到另一个位置,而没有首先考虑到这些重复文件会消耗越来越多的存储空间。我想在这篇博文中与您一起改变这一点。我们将搜索重复的文件,然后将它们移动到不同的存储位置以供进一步审查。

目标

有了我的脚本,您就可以执行所描述的场景。确保您的计算机运行 Windows PowerShell 5.1 或 PowerShell 7。

  1. 打开 PowerShell(Windows 键 + X + A)
  2. 导航到脚本位置。输入目标文件夹的完整路径。该文件夹是我们搜索重复文件的目标

    [玩转系统] Windows:使用 PowerShell 查找并消除重复文件

  3. 将弹出一个窗口,根据哈希值选择重复文件。所有选定的文件将移动C:\DuplicatesCurrentDate

    [玩转系统] Windows:使用 PowerShell 查找并消除重复文件

  4. 然后,重复文件将移至新位置。您将再次看到一个新窗口出现,其中显示移动的文件以供进一步查看

    [玩转系统] Windows:使用 PowerShell 查找并消除重复文件

这让我想到了代码。

剧本

这是下载代码。

查找重复文件.ps1

这是完整的代码。将代码复制到本地计算机,然后在 PowerShell ISE、Visual Studio Code 或您选择的编辑器中打开它。按 F5(PowerShell ISE 或 VS Code)。


# .SYNOPSIS
# find_ducplicate_files.ps1 finds duplicate files based on hash values.

# .DESCRIPTION
# Prompts for entering file path. Shows duplicate files for selection.
# Selected files will be moved to new folder C:\Duplicates_Date for further review.

# .EXAMPLE
# Open PowerShell. Nagivate to the file location. Type .\find_duplicate_files.ps1 OR
# Open PowerShell ISE. Open find_duplicate.ps1 and hit F5.

# .NOTES
# Author: Patrick Gruenauer | Microsoft MVP on PowerShell [2018-2020]
# Web: https://a-d.site

############# Find Duplicate Files based on Hash Value ###############
''
$filepath = Read-Host 'Enter file path for searching duplicate files (e.g. C:\Temp, C:\)'

If (Test-Path $filepath) {
''
Write-Warning 'Searching for duplicates ... Please wait ...'

$duplicates = Get-ChildItem $filepath -File -Recurse `
-ErrorAction SilentlyContinue |
Get-FileHash |
Group-Object -Property Hash |
Where-Object Count -GT 1

If ($duplicates.count -lt 1)

{
Write-Warning 'No duplicates found.'
Break ''
}

else {
Write-Warning "Duplicates found."
$result = foreach ($d in $duplicates)
{
$d.Group | Select-Object -Property Path, Hash
}

$date = Get-Date -Format "MM/dd/yyy"
$itemstomove = $result |
Out-GridView -Title `
"Select files (CTRL for multiple) and press OK. Selected files will be moved to C:\Duplicates_$date" `
-PassThru

If ($itemstomove)

{
New-Item -ItemType Directory `
-Path $env:SystemDrive\Duplicates_$date -Force
Move-Item $itemstomove.Path `
-Destination $env:SystemDrive\Duplicates_$date -Force
''
Write-Warning `
"Mission accomplished. Selected files moved to C:\Duplicates_$date"

Start-Process "C:\Duplicates_$date"
}

else
{
Write-Warning "Operation aborted. No files selected."
}
}
}
else
{
Write-Warning `
"Folder not found. Use full path to directory e.g. C:\photos\patrick"
}

制作人员

感谢肯沃德·布拉德利的一句台词激发了我写这个剧本的想法。干得好:

http://kenwardtown.com/2016/12/29/find-duplicate-files-with-powershell/

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯