【问题标题】:Get number of unique extensions from a directory using powershell使用 powershell 从目录中获取唯一扩展的数量
【发布时间】:2021-10-06 22:05:32
【问题描述】:

我正在尝试制作一个可以放入目录中的 powershell 脚本,然后返回所有唯一的扩展名及其各自的计数。根据找到的扩展名,我可以输出该信息。每当我以当前状态运行脚本时,都会创建并导入 CSV,但我似乎无法将变量从 CSV 传回。如果我创建(Extension = "something"),即使它不应该输出,它也会始终输出为真,例如下面代码中显示的 .jsp 的情况,它不在目录中并且不应该返回任何内容。我也不确定如何测量每个扩展的计数,因为Measure-Object 似乎没有正确触发。

我的目标如下:

  1. 获取所有独特的扩展及其数量,然后放入 CSV

  2. 从 CSV 导入所有唯一的扩展名并输出一条消息,如果 找到特定的扩展名及其次数 出现

$path = "C:\Users\spawn\Desktop\Powershell"

gci -rec $path -exclude *.ps1, *.csv, *.bat | Select-Object Extension -u | Export-Csv .\Export.csv
#Below is an example of how I think it should work
#gci -rec $path -exclude *.ps1, *.csv, *.bat | Measure-Object Extension | Export-Csv .\Export.csv

$escv = Import-Csv "C:\Users\spawn\Desktop\Powershell\Export.csv" 

ForEach ($data in $escv)
{
    $Count = $data.count
    $Extension = $data.extension
    
    if ($Extension -eq ".cs")
    {
        echo "A .cs file was found."
        echo "There were $Count files found."
    }
    if ($Extension -eq ".sln")
    {
        echo "A .sln file was found."
        echo "There were $Count files found."
    }
    if ($Extension -eq ".java")
    {
        echo "A .java file was found."
        echo "There were $Count files found."
    }
    if ($Extension -eq ".jsp")
    {
        echo "A .jsp file was found."
        echo "There were $Count files found."
    }
    return

}

【问题讨论】:

    标签: powershell csv


    【解决方案1】:

    使用哈希:

    dir | % { $hash = @{} } { $hash[$_.extension]++ }
    $hash
    
    Name                           Value
    ----                           -----
    .exe                           5
    .txt                           26
    .vbs                           1
    .vbs~                          1
    .ps1#                          1
    .nbi                           1
    .spss                          1
    .csv                           7
    .android                       1
    .csv#                          1
    .json                          5
    .bat                           5
    .json~                         3
    .iso                           1
    .cisco                         1
    .ps1                           22
    .matplotlib                    1
    .vscode                        1
    .bat~                          1
    .d                             1
    .ps1~                          13
    .eclipse                       1
    .log                           50
    .oracle_jre_usage              1
    .emacs                         1
    .xml~                          1
    .tooling                       1
    .pyosf                         1
    

    【讨论】:

      【解决方案2】:

      是否有理由处理所有可能的扩展而不是遍历列表?

      Group-Object 为您提供没有循环的简单计数。

      您可以重复使用相同的变量来写入 csv 和屏幕。

      $Path = 'C:\Users\spawn\Desktop\Powershell'
      $ExcludeExtensions = @('*.ps1', '*.csv', '*.bat')
      
      $Counts = Get-ChildItem -Recurse $Path -Exclude $ExcludeExtensions |
          Group-Object -Property Extension |
          Select-Object -Property @('Count', 'Name')
          
      $Counts.ForEach{
          Write-Output "A $($_.Name) file was found"
          Write-Output "There were $($_.Count) files found"
      }
      
      $Counts | Export-Csv -LiteralPath (Join-Path $Path 'Export.csv')
      

      【讨论】:

      • 很好,但我认为为了简洁和性能,最好坚持使用-Exclude *.ps1, *.csv, *.bat
      猜你喜欢
      • 1970-01-01
      • 2014-07-12
      • 2017-04-09
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多