简体   繁体   中英

How can I use the following cmd for multiple files?

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"
    }
}

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"
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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