Powershell 如何在哈希表末尾添加键/值对?

Powershell 如何在哈希表末尾添加键/值对?,powershell,powershell-2.0,Powershell,Powershell 2.0,我正在尝试使用PowerShell脚本计算代码计数 我在网上找到了一个脚本,并试图在最后添加一行 Param( [string]$path, [string]$outputFile, [string]$include = "*.*", [string]$exclude = "") Clear-Host $Files = Get-ChildItem -re -in $include -ex $exclude $path $CountHash = @{}

我正在尝试使用PowerShell脚本计算代码计数

我在网上找到了一个脚本,并试图在最后添加一行

Param( [string]$path,
       [string]$outputFile,
       [string]$include = "*.*",
       [string]$exclude = "")

Clear-Host

$Files = Get-ChildItem -re -in $include -ex $exclude $path
$CountHash = @{}
$Total=0
Foreach ($File in $Files) {
    #Write-Host "Counting $File.FullName"
    $fileStats = Get-Content $File.FullName | Measure-Object -line
    $linesInFile = $fileStats.Lines
    $CountHash.Add($File.FullName, $linesInFile)

    $Total += $linesInFile
}

$CountHash.Add("Total", $Total)
$CountHash
我已经添加了这个专栏

$CountHash.Add("Total", $Total)
最后

Param( [string]$path,
       [string]$outputFile,
       [string]$include = "*.*",
       [string]$exclude = "")

Clear-Host

$Files = Get-ChildItem -re -in $include -ex $exclude $path
$CountHash = @{}
$Total=0
Foreach ($File in $Files) {
    #Write-Host "Counting $File.FullName"
    $fileStats = Get-Content $File.FullName | Measure-Object -line
    $linesInFile = $fileStats.Lines
    $CountHash.Add($File.FullName, $linesInFile)

    $Total += $linesInFile
}

$CountHash.Add("Total", $Total)
$CountHash

但是,当我显示$COUNTHASH时,它在中间显示“总”键。如果在末尾添加Add,则无法确保在末尾添加它

Param( [string]$path,
       [string]$outputFile,
       [string]$include = "*.*",
       [string]$exclude = "")

Clear-Host

$Files = Get-ChildItem -re -in $include -ex $exclude $path
$CountHash = @{}
$Total=0
Foreach ($File in $Files) {
    #Write-Host "Counting $File.FullName"
    $fileStats = Get-Content $File.FullName | Measure-Object -line
    $linesInFile = $fileStats.Lines
    $CountHash.Add($File.FullName, $linesInFile)

    $Total += $linesInFile
}

$CountHash.Add("Total", $Total)
$CountHash
如何在哈希表的末尾添加键/值对


我把这个哈希表导出为CSV文件,但是总的行在中间。

假定总数只是用于显示,我想将它添加到哈希集是没有意义的。 拆下线路

$CountHash.Add("Total", $Total)
并将其添加为最后一行:

Write-Host "Total: $Total"

假设总数只是为了显示,我想把它添加到散列集中是没有意义的。 拆下线路

$CountHash.Add("Total", $Total)
并将其添加为最后一行:

Write-Host "Total: $Total"

要回答您的问题,可以使用Add方法,或通过指定新键来创建新键:

$CountHash.Total = $Total
但是,我会采取更简单的方法,自定义对象而不是哈希表:

Get-ChildItem -Path $path -Include $include -Exclude $exclude -Recurse |
Select-Object FullName, @{Name='LineCount';Expression={ (Get-Content $_.FullName | Measure-Object -Line).Lines}} |
Export-Csv .\files.csv

要回答您的问题,可以使用Add方法,或通过指定新键来创建新键:

$CountHash.Total = $Total
但是,我会采取更简单的方法,自定义对象而不是哈希表:

Get-ChildItem -Path $path -Include $include -Exclude $exclude -Recurse |
Select-Object FullName, @{Name='LineCount';Expression={ (Get-Content $_.FullName | Measure-Object -Line).Lines}} |
Export-Csv .\files.csv
我会这样做:

$CountHash += @{Total = $total}
我会这样做:

$CountHash += @{Total = $total}

哈希表不维护其值的顺序。如果您想要具有顺序的类似数据结构,请尝试使用
System.Collection.Specialized.OrderedDictionary
。您的示例将如下所示

$Files=Get-ChildItem -re -in $include -ex $exclude $path
$CountHash= New-Object System.Collections.Specialized.OrderedDictionary # CHANGED
$Total=0
Foreach ($File in $Files) { 
   #Write-Host "Counting $File.FullName"
   $fileStats = Get-Content $File.FullName | Measure-Object -line
   $linesInFile = $fileStats.Lines
   $CountHash.Add($File.FullName,$linesInFile)

   $Total += $linesInFile
}

$CountHash.Add("Total",$Total)
$CountHash

哈希表不维护其值的顺序。如果您想要具有顺序的类似数据结构,请尝试使用
System.Collection.Specialized.OrderedDictionary
。您的示例将如下所示

$Files=Get-ChildItem -re -in $include -ex $exclude $path
$CountHash= New-Object System.Collections.Specialized.OrderedDictionary # CHANGED
$Total=0
Foreach ($File in $Files) { 
   #Write-Host "Counting $File.FullName"
   $fileStats = Get-Content $File.FullName | Measure-Object -line
   $linesInFile = $fileStats.Lines
   $CountHash.Add($File.FullName,$linesInFile)

   $Total += $linesInFile
}

$CountHash.Add("Total",$Total)
$CountHash

我已经更新了我的问题。对不起,我不只是显示它,我已经更新了我的问题。对不起,我不是在展示它