【问题标题】:Find Zip file and a folder that have same name查找具有相同名称的 Zip 文件和文件夹
【发布时间】:2023-01-11 02:31:07
【问题描述】:

我正在尝试通过 PowerShell 或其他方法查找具有相同名称的 zip 文件和文件夹。

工作人员在我们的存储阵列上解压缩他们的 zip 文件,随后永远不会删除该 zip 文件。现在我们有两个副本,zip 文件和解压缩的文件夹。

通过 PowerShell,我可以找到具有相同名称的文本和 zip 文件,但找不到文件夹和 zip 文件。

任何帮助表示赞赏。下面是我的代码。

Get-ChildItem -Path c:\temp\test -Recurse | Group-Object -Property Directory, BaseName | Where-Object Count -gt 1 | Select-Object Name

【问题讨论】:

  • 只是为了确认我理解正确,您要查找包含 zip 文件和具有与 zip 相同基本名称的子文件夹的任何文件夹是否正确?
  • 你想要更大等于 1,而不是更大。希望您只会得到一个匹配的。
  • Get-ChildItem -File -Recurse -Filter '*.zip' | ForEach-Object {$Dpath = Join-Path -Path $_.DirectoryName -ChildPath $_.BaseName; if (Test-Path -Path $Dpath) { $Dpath }}

标签: powershell


【解决方案1】:

假设我理解正确,这是您可以做到的一种方法。请参阅内联 cmets 以帮助您理解逻辑。

# find all `.zip` files recursively
Get-ChildItem c:	emp	est -Filter *.zip -Recurse |
    # and filter where the Parent Directory of this file also has a sub-folder (at least 1)
    # with the same `.BaseName` as this `.zip` file :)
    Where-Object { $_.Directory.EnumerateDirectories($_.BaseName) | Select-Object -First 1 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多