List last row from >2000 txt files RRS feed

  • Question

  • Hello dear powershell scripting god

    I try to write a script that lists the last row in several txt files.
    I have a folder with about 2500 txt files and I try to list the last row of every file.
    Additional I should also know the Name of the file in which the row exists.

    The Output should look like:
    File1.txt, text of last row
    File2.txt, text of last row

    I have tried this script, but it did not work. Why?

    Get-ChildItem \\stg-w59\PCsSTGAG\logins\computer\*.txt | ft name | Out-File C:\Temp-DG\liste.txt
    get-content c:\temp-dg\liste.txt | select -skip 3 > C:\Temp-DG\Dateiliste.txt
    $DateiAray = C:\Temp-DG\Dateiliste.txt
    foreach ($Datei in $DateiAray) {
    Get-Content \\stg-w59\PCsSTGAG\logins\computer\$Datei | Select-Object -Last 1

    Can anybody help me with this script?

    Thursday, July 30, 2015 4:22 PM


All replies

  • Hi,

    It did not work because you're not loading the file list properly, you're not writing anything to an output file, and you're not doing anything to capture the file names.

    I suggest using Get-ChildItem to get your list of files, piping through ForEach-Object, using Get-Content to read your file, creating a custom object with the filename and data, and then exporting once you've completed your loop.

    Thursday, July 30, 2015 4:28 PM
  • get-content \\stg-w59\PCsSTGAG\logins\computer\*.txt -Tail 1


    get-childitem *.txt |%{[pscustomobject]@{File=$_.Name;Lastline=($_|get-content -Tail 1)}}



    Thursday, July 30, 2015 4:48 PM
  • Tanks jrv

    Your second commandline "get-childitem..." is exactly what I need.

    You saved my day. :-)

    Friday, July 31, 2015 6:06 AM