Dear Abby: My life is all messed up and I need to do something about it. How can I turn my life around and be happy again? Desperate in Dayton. So how do you think the Scripting Guys will answer your question about reading just the last line of a text file? You got it:. It has to start at the beginning of a file, and it can only continue on towards the end of that file. The workaround here is that we actually do read the entire script from beginning to end. When we echo back the value of that variable it will look like we read only the last line especially because, if nothing else, the FileSystemObject is pretty fast.
We then create an instance of the Scripting. That brings us to this block of code:. Each time we read in a line we replace the value of the variable strLine with the text we just read. For example, suppose our text file consists of three lines:.
This isn't last 10 lines. It's all lines since line number This works under Windows 7, both 64 and 32 bit consoles. PatrickCuff, oh dammit, sorry, you're right, once again I win the pie. This answer combines the best features of already existing answers, and adds a few twists. The solution is a simple batch implementation of the tail command. The second argument is the number of lines to print. I am trying to use this for tail -f kind of functionality. AbibullahRahamathulah - No, there is no equivalent to the tail -f option.
I have started using the tail. Thanks for your response. For some reason this script counted only lines when the file was actually lines long. Show 5 more comments. Hm, that's the kind of straightforward way I never really consider with batch files. But yes, it nibbles away at the eyes. Nice one… but will output blank lines if input file is less than 10 lines long. This is an interesting brute force approach that can work nicely if the files are cooperative.
MattiasAndersson That gotcha is avoided with echo. Phil Phil 5 5 silver badges 18 18 bronze badges. Hassan Hassan. Credit goes to OP Peter Mortensen. Community Bot 1 1 1 silver badge. Randy Rakestraw Randy Rakestraw 1 1 silver badge 10 10 bronze badges.
Eyal Eyal 1 1 silver badge 9 9 bronze badges. You could also create a batch file named TAIL. Why go through the pain of calling PowerShell from cmd. Just use PowerShell interactively instead of cmd. PollusB PollusB 1, 2 2 gold badges 20 20 silver badges 29 29 bronze badges. Script last. Here -tail 1 show the bottom line of the file. Cool Tip: Do you know the equivalent of cat command in Windows! In the above command, PowerShell Get-Content cmdlet -tail parameter gets last 50 lines of the code.
Using pipe operator, it passed extracted lines to Out-File for export to csv file. Cool Tip: Using PowerShell to get first line of file! Another popular use of tail parameter is to get last lines from the file.
0コメント