简体   繁体   English

列出文件夹中最大行数的.txt文件(文件),然后列出最小行数的txt文件(文件)

[英]List .txt file (files) with maximum number of lines in a folder and then txt file (files) with minimum number of lines

I have this code, but it is not working properly 我有此代码,但无法正常工作

$minLines = 1
$maxLines = 1000
Get-ChildItem . -Filter "*.txt" -Recurse |
    Where-Object {
        $numLines = Get-Content $_.FullName |
            Measure-Object -Line |
            Select-Object -ExpandProperty Lines
        if (($numLines -gt $minLines) -and ($numLines -lt $maxLines)) {
            return $_
        }
    }

I have in one directory hundreds of text files some of them has over 700 lines some just 4 or 5. Sometimes min or max criteria meet dozens of files with same number of lines.I would like at first list file or files with minimum number of lines and than file or files(if more than one) with max number of lines. 我在一个目录中有数百个文本文件,其中一些文件有700多行,而其中只有4或5条。有时,最小或最大标准可以满足数十个具有相同行数的文件。行,并且文件数超过上限的文件(如果超过一个)。

Probably at first in $minLines and $maxLines shuld be stored somehow maximum and minimum number of lines of all text files of a directory and then list file or files matching minimum number of lines and then maximum number of lines. 可能首先应该以某种方式存储$ minLines和$ maxLines中目录的所有文本文件的最大和最小行数,然后列出匹配最小行数和最大行数的文件。

I've also find this piece of code: 我也找到了这段代码:

dir . -filter "*.txt" -Recurse -name | foreach{(GC $_).Count} | measure-object -max -min

It can by useful too. 它也可能有用。 This code give us information about max and min number of lines in a folder. 此代码为我们提供了有关文件夹中最大和最小行数的信息。

You could go with something like this one-liner: 您可以使用这种单线格式:

Get-ChildItem . *.txt -Recurse | Select FullName, @{n="NumLines";e={(gc $_).count}} | 
    Sort NumLines | Group NumLines | Select -First 1 -Last 1

To see the full names of each file: 要查看每个文件的全名:

Get-ChildItem . *.ps1 -Recurse | Select FullName, @{n="NumLines";e={(gc $_).count}} |
    Sort NumLines | Group NumLines | Select -First 1 -Last 1 -ExpandProperty Group

This will set the min and the max values: 这将设置最小值和最大值:

Get-ChildItem C:\Users\Public\Documents\Test\ | % {
    $lines = (Get-Content $_.FullName).Count
    if (($min -eq $null) -and ($max -eq $null)) {
        $min=$lines
        $max=$lines
        }
    if ($lines -lt $min) {
        $min=$lines
        }
    if ($lines -gt $max) {
        $max=$lines
        }
    }

This will get the files with the maximum number of lines: 这将获得具有最大行数的文件:

Get-ChildItem C:\Users\Public\Documents\Test\ | ? {(Get-Content $_.FullName).Count -eq $max}

And this will get the files with the minimum: 这将获得最少的文件:

Get-ChildItem C:\Users\Public\Documents\Test\ | ? {(Get-Content $_.FullName).Count -eq $min}

If you want the filenames and the number of lines you could append this to the maximum line or modify it slightly to get the minimum: 如果您需要文件名和行数,则可以将其附加到最大行数或对其进行稍微修改以获取最小数:

| % {("File " + $_.FullName + " has " + $max + "lines.")}

From this you can use Out-File to get it to a .txt file. 由此,您可以使用Out-File将其保存为.txt文件。

| Out-File C:\max_lines.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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