Reputation: 23
The following function allows me to list detailed data for a single file, what can I have to do to get it to run, if there are multiple files in a variable.
function Text ($file) {
try {
$content = Get-Content $ASCIIfile
$text = $content | out-string
$result = [regex]::matches($text, '\b\S+\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"
}
}
Upvotes: 1
Views: 178
Reputation: 174445
Pipe the input to ForEach-Object
, and replace the previous variable reference to $file
with $_
($_
refers to the current input item received by ForEach-Object
):
function Text ($file) {
$file | ForEach-Object {
try {
$content = Get-Content $_
$text = $content | out-string
$result = [regex]::matches($text, '\b\S+\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"
}
}
}
Upvotes: 2