【问题标题】:Recurse with PowerShell's Get-ChildItem使用 PowerShell 的 Get-ChildItem 进行递归
【发布时间】:2014-06-01 02:04:34
【问题描述】:

这看起来应该很简单,我敢肯定它是,但我没有破解它应该完成的最佳方式。

我想搜索一个文件夹结构,返回满足以下条件的文件夹。

  1. 包含 .msi 文件但不包含可执行文件的文件夹。

  2. 包含 .exe 文件但不包含 .msi 文件的文件夹。

  3. 同时包含 exe 和 msi 文件的文件夹。

其中的每一个都将通过管道传送到 CSV 文件中的列。

我的问题是我不知道如何有效地返回包含一种文件类型但排除另一种文件类型的文件夹名称。我知道在纸上使用-include *.msi-exclude *.exe 等似乎很简单,但是像gci -Recurse -Include *.msi -Exclude *.exe 这样的命令包括包含 msi 和 exe 文件夹的文件夹,我只希望返回包含 msi 的文件夹。

我正在使用以下目录结构作为测试

只有微星

msi 和 exe

仅可执行文件

这有意义吗?

我正在尝试| where directory -notcontains *.exe 和各种类似的东西,但它们都没有按我想要或预期的方式工作。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    不幸的是,包含和排除仅适用于递归参数。如果没有递归,您将无法进行排除。

    这是另一种选择。

    $folders = dir -path ~ -recurse | ? {$_.PsIsContainer}
    $folders | ForEach-Object {
         $exe_files = $_ | dir -filter *.exe
         $msi_files = $_ | dir -filter *.msi
         $type = ''
         if ($exe_files -and $msi_files) {$type = 'Both'}
         if ($msi_files -and -not $exe_files) {$type = 'MSI_ONLY'}
         if ($exe_files -and -not $msi_files) {$type = 'EXE_ONLY'}
         if ($type) {
              New-Object -TypeName PsObject -Property @{Path=$_.FullName;Type="$type"}
         }
    } | ConvertTo-Csv | Set-Content ~\out.csv
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-26
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多