需要添加引用测试的完整路径
Need to add the full path of where test was referenced from
到目前为止,我有一个包含 2 个值的散列 table。现在下面的代码导出所有唯一的行,并告诉我该行在 100 个 xml 文件中被引用了多少次。这是一部分。
我现在需要找出哪个子文件夹中有 xml 文件,该文件在哈希 table 中引用了唯一行。这可能吗?
$ht = @{}
Get-ChildItem -recurse -Filter *.xml | Get-Content | %{$ht[$_] = $ht[$_]+1}
$ht
# To export to CSV:
$ht.GetEnumerator() | select key, value | Export-Csv D:\output.csv
要获取输出的文件路径,您需要将其分配给第一个管道中的变量。
这是否与您需要的类似?
$ht = @{}
Get-ChildItem -recurse -Filter *.xml | %{$path = $_.FullName; Get-Content $path} | % { $ht[$_] = $ht[$_] + $path + ";"}
上面的代码将 return 哈希-table "config line" = "count" 格式。
编辑:
如果您需要 return 三个元素(唯一的行、计数和找到它的路径数组),它会变得更加复杂。这是将 return PSObjects 数组的代码。每个都包含 XML 个文件中一行的信息。
$ht = @()
$files = Get-ChildItem -recurse -Filter *.xml
foreach ($file in $files) {
$path = $file.FullName
$lines = Get-Content $path
foreach ($line in $lines) {
if ($match = $ht | where {$_.line -EQ $line}) {
$match.count = $match.count + 1
$match.Paths += $path
} else {
$ht += new-object PSObject -Property @{
Count = 1
Paths = @(,$path)
Line = $line }
}
}
}
$ht
我确信它可以缩短和优化,但希望它足以让您入门。
到目前为止,我有一个包含 2 个值的散列 table。现在下面的代码导出所有唯一的行,并告诉我该行在 100 个 xml 文件中被引用了多少次。这是一部分。
我现在需要找出哪个子文件夹中有 xml 文件,该文件在哈希 table 中引用了唯一行。这可能吗?
$ht = @{}
Get-ChildItem -recurse -Filter *.xml | Get-Content | %{$ht[$_] = $ht[$_]+1}
$ht
# To export to CSV:
$ht.GetEnumerator() | select key, value | Export-Csv D:\output.csv
要获取输出的文件路径,您需要将其分配给第一个管道中的变量。
这是否与您需要的类似?
$ht = @{}
Get-ChildItem -recurse -Filter *.xml | %{$path = $_.FullName; Get-Content $path} | % { $ht[$_] = $ht[$_] + $path + ";"}
上面的代码将 return 哈希-table "config line" = "count" 格式。
编辑:
如果您需要 return 三个元素(唯一的行、计数和找到它的路径数组),它会变得更加复杂。这是将 return PSObjects 数组的代码。每个都包含 XML 个文件中一行的信息。
$ht = @()
$files = Get-ChildItem -recurse -Filter *.xml
foreach ($file in $files) {
$path = $file.FullName
$lines = Get-Content $path
foreach ($line in $lines) {
if ($match = $ht | where {$_.line -EQ $line}) {
$match.count = $match.count + 1
$match.Paths += $path
} else {
$ht += new-object PSObject -Property @{
Count = 1
Paths = @(,$path)
Line = $line }
}
}
}
$ht
我确信它可以缩短和优化,但希望它足以让您入门。