下面的函数允许我列出单个文件的详细数据,如果一个变量中有多个文件,我该怎么做才能使它运行
function Text ($file) {
try {
$content = Get-Content $ASCIIfile
$text = $content | out-string
$result = [regex]::matches($text, 'bS+b')
$statistik = $result | Select-Object -Expand Captures | Group-Object Value -NoElement | Sort-Object Count -Descending
$numbers = @{
'Anzahl Wörter' = $result.Count
'Anzahl Zeilen' = $content.Count
'Zeichen (mit Leerzeichen)' = ($text.Length - 2)
'Zeichen (ohne Leerzeichen)' = ($text -replace 's', '').Length
}
$('=' * 20)
$($numbers | Format-Table -HideTableHeaders | out-string)
}
catch {
write-host "Falsche Eingabe"
}
}
将输入输入到ForEach-Object
,并将之前对$file
的变量引用替换为$_
($_
表示ForEach-Object
接收到的当前输入项):
function Text ($file) {
$file | ForEach-Object {
try {
$content = Get-Content $_
$text = $content | out-string
$result = [regex]::matches($text, 'bS+b')
$statistik = $result | Select-Object -Expand Captures | Group-Object Value -NoElement | Sort-Object Count -Descending
$numbers = @{
'Anzahl Wörter' = $result.Count
'Anzahl Zeilen' = $content.Count
'Zeichen (mit Leerzeichen)' = ($text.Length - 2)
'Zeichen (ohne Leerzeichen)' = ($text -replace 's', '').Length
}
$('=' * 20)
$($numbers | Format-Table -HideTableHeaders | out-string)
}
catch {
write-host "Falsche Eingabe"
}
}
}