counting the number of lines for 100 text files

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #6086

    by sukans1995 at 2013-05-03 13:21:13

    I would like an easy way to find out for a given path along with filters like "c:\windows|*.txt" as a parameter, the number of lines in each file, along with filename

    Output will be like

    Filename Total lines
    ===========================
    name1.txt 300
    name2.txt 10
    name3.txt 40

    Thanks
    Sukanya

    by ArtB0514 at 2013-05-03 13:50:27

    #requires PowerShell V3.0
    $FileData = @()
    Get-ChildItem c:\Windows -Include *.txt | ForEach-Object {
    $FileData += [pscustomobject]@{'FileName'=$_.Name;'Total Lines'=(Get-Content $_.FullName).Count}
    }

    or this for any version of PowerShell:
    $FileData=@()
    Get-ChildItem C:\Windows -Include *.txt | ForEach-Object {
    $FileData += New-Object PSObject -Property @{'FileName'=$_.Name;'Total Lines'=(Get-Content $_.FullName).Count}
    }

You must be logged in to reply to this topic.