0

Can anyone please advise how to use Windows Command Prompt to read last 10 lines of a text file?

Greenbanana
  • 21
  • 1
  • 1
  • 1
    Possible duplicate of [How do you loop through each line in a text file using a windows batch file?](https://stackoverflow.com/questions/155932/how-do-you-loop-through-each-line-in-a-text-file-using-a-windows-batch-file) – Stavm Jun 16 '17 at 07:49
  • 2
    Possible duplicate of [CMD.EXE batch script to display last 10 lines from a txt file](https://stackoverflow.com/questions/523181/cmd-exe-batch-script-to-display-last-10-lines-from-a-txt-file) – Compo Jun 16 '17 at 09:31

2 Answers2

1

I use this: Get-Content $args[0] -Tail $args[1] -Wait

Usage: tail.ps1 [fileName] [lastNumberOfLines]

It is designed for log files to scroll as they are being written. You can remove the -Wait if you just want to see the last lines of a file that isn't changing.

0

One way is to use PowerShell.

powershell -NoProfile -Command "Get-Content .\rrreg.ps1 | Select-Object -Last 3"

Iterating across each line can also be done.

FOR /F "usebackq tokens=*" %%s IN (`powershell -NoProfile -Command "Get-Content .\rrreg.ps1 | Select-Object -Last 3"`) DO (
    ECHO %%s
)

Of course, having a tail command on your system would make tail -10 thefile.txt possible. How much easier can it be than that?

lit
  • 14,456
  • 10
  • 65
  • 119
  • someone voted this down, but it's actually a good answer. I've tried it on a 4Gb file. Took a while to get to the last 3 lines, but did it – Ricardo Appleton Jan 04 '23 at 12:11
  • adding to my previous comment, I tried it on a 4Gb file, but ran the command from a powershell window, so only ran the part "Get-Content .\YourFileHere | Select-Object -Last 3", without the quotes obviously – Ricardo Appleton Jan 04 '23 at 17:00