简体   繁体   English

使用 powershell 从目录中获取唯一扩展名的数量

[英]Get number of unique extensions from a directory using powershell

I'm trying to make a powershell script that can be placed into a directory and then return all of the unique extensions and their respective counts.我正在尝试制作一个 powershell 脚本,该脚本可以放入一个目录中,然后返回所有唯一扩展名及其各自的计数。 Based on which extensions are found then I can output that information back.根据找到的扩展名,我可以 output 返回该信息。 Whenever I run the script in its current state the CSV is created and imported but I cannot seem to pass the variables back from the CSV. If I make (Extension = "something") it will always output as true even when it shouldn't be, such is the case shown in code below for.jsp which is not in the directory and should not return anything.每当我在其当前 state 中运行脚本时,都会创建并导入 CSV,但我似乎无法将变量从 CSV 传回。如果我制作(Extension = "something") ,即使它不应该将 output 始终设为 true是的,下面代码中显示的情况就是这样。jsp 不在目录中,不应返回任何内容。 I am also unsure of how to measure the count of each extension as Measure-Object doesn't seem to be triggering properly.我也不确定如何测量每个扩展的计数,因为Measure-Object似乎没有正确触发。

My goals are as follows:我的目标如下:

  1. Grab all unique extensions and their count then place into CSV获取所有唯一的扩展名及其计数,然后放入 CSV

  2. Import from CSV all unique extensions and output a message if a specific extension is found along with the number of times it appears从 CSV 导入所有唯一扩展名和 output 如果找到特定扩展名及其出现次数,则导入一条消息

$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

}

Is there a reason to handle all possible extensions rather than loop through the list?是否有理由处理所有可能的扩展而不是遍历列表?

Group-Object gives you simple counts without loops. Group-Object为您提供没有循环的简单计数。

You can re-use the same variable for writing to csv and screen.您可以重新使用相同的变量写入 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')

With a hash:使用 hash:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM