How to Flip or Reverse a Text File Using Different Ways in Windows

When analyzing plain-text log files such as the Web server logs or the Sfc, or DISM log files, you may sometimes prefer the most recent items appearing at the top. However, most of the log files that Windows generates would have the oldest entries listed first — entries listed in chronological order.

reverse lines in a text file windows

You may be wondering how to reverse the lines in a log file (or any plain-text file, for that matter) so that the newest items appear at the top. You can reverse the lines in a text file using several methods, of which some of them are covered in this article.

Note: As always, before proceeding, be sure to backup the original text file before manipulating it — e.g., using find/replace, or running a script or macro against a text file.

Reverse a Text File Using Different Ways in Windows

Method 1: Using online portals (free)

There are several free online services to reverse the order of the lines in a text file for you. Here is a couple of useful sites:

Sort Text Lines - Text Mechanic:
Big File Tool - Sort Lines:
Reverse Lines:

reverse lines in a text file windows

Method 2: Using PowerShell

To reverse the order of the lines in a text file using PowerShell, use this command-line syntax from a PowerShell window:

$x = Get-Content -Path "C:\Users\ramesh\Desktop\dism.log"; Set-Content -Path "C:\Users\ramesh\Desktop\dism.log" -Value ($x[($x.Length-1)..0])

reverse lines in a text file windows powershell

Method 3: Using VBScript

Make a VBScript file from the following contents. To do so, open Notepad, copy the following lines into it and save the file with .vbs extension — e.g. reverse_text_file.vbs

'Script to reverse the lines in a text file. Takes a text file name as the argument.
If Wscript.arguments.count = 0 Then wscript.quit
Dim sFileName
sFileName = Wscript.arguments(0)
Const ForReading = 1
Const ForWriting = 2
Const TriStateTrue = -1
Dim arrLines()
i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(sFileName, ForReading, TriStateTrue)
Do Until objFile.AtEndOfStream
   ReDim Preserve arrLines(i)
   arrLines(i) = objFile.ReadLine
   i = i + 1
Set objFile = objFSO.OpenTextFile(sFileName, ForWriting, TriStateTrue)
For i = Ubound(arrLines) To LBound(arrLines) Step -1
   objFile.WriteLine arrLines(i)
Wscript.echo "Done"

To run the VBScript file, use the following command-line syntax:

cscript //nologo reverse_text_file.vbs "d:\logs\somefile.txt"


wscript reverse_text_file.vbs "d:\logs\somefile.txt"

Alternately, drag the text file on the VBScript file to reverse the text file contents.

You may also place a VBScript shortcut to the Send To folder for easier access. In the Send To folder of your user account, create a shortcut to the VBScript with wscript.exe prefix — e.g. wscript.exe d:\reverse-text-file.vbs and customize its icon and shortcut caption.

reverse lines in a text file windows vbscript

Method 4: Using Notepad++

To reverse the lines in a text file using Notepad++, first, you’ll need to add serial number prefix for each line. Then reverse the lines (lexicographically descending), and then finally remove the serial numbers. Follow these steps:

  1. Open the text file using Notepad++
  2. Select all the contents via Edit → Select All
  3. From the Edit menu, select Column Editor
    reverse lines in a text file notepad++
  4. Select the Number to Insert radio button.
  5. Set Initial number: to 1
  6. Set Increase by: to 1
  7. Enable Leading zeros, and click OK. This adds the line number or the serial number before each line.
    reverse lines in a text file notepad++
  8. From the Edit menu, click Line OperationsSort Lines Lexicographically Descending.
    reverse lines in a text file notepad++
    The lines in the text file are now reversed. The total number of lines in this example is 17612 (i.e. 5 digits). Now, you’ll need to remove the first five characters (Sl. no) in each line.
  9. Go to the 1st line in the text file, and press Ctrl + H to launch the Find & Replace dialog.
  10. In the Find what: text box, type ^.{5}(.*)$
  11. In the Replace with: box, type $1
  12. Set the Search Mode to Regular expression
    reverse lines in a text file notepad++
  13. Click Replace All. That’s it. The first five characters are removed.
    reverse lines in a text file notepad++
  14. Save the file and close Notepad++.

This reverses the lines in your text file!

One small request: If you liked this post, please share this?

One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
  • Pin it!
  • Share it to your favorite blog + Facebook, Reddit
  • Tweet it!
So thank you so much for your support, my reader. It won't take more than 10 seconds of your time. The share buttons are right below. :)

About the author

Ramesh Srinivasan founded back in 2005. He is passionate about Microsoft technologies and he has been a Microsoft Most Valuable Professional (MVP) for 10 consecutive years from 2003 to 2012.

Leave a Comment